0

I'm trying to write a class handling multiplication, division, etc of polynomials.

In my header file, I have my class definition, in which I have those public functions :

void Poly_Mult(const Polynomial& Poly);
void Poly_Divide(const Polynomial& Poly);

and also two friend functions :

friend Polynomial Poly_Mult(const Polynomial& Poly1, const Polynomial& Poly2);
friend Polynomial Poly_Divide(const Polynomial& Poly1, const Polynomial& Poly2);

I have defined both Poly_Mult functions with no problem and I try to use them to define the Poly_Divide ones. For the friend function, I have no problem. However, in the member function :

bool Polynomial::Poly_Divide(const Polynomial& Poly)
{
    if (Poly.m_Degree == 0)
        return false;

    Polynomial result;
    result.m_Degree = this->m_Degree - Poly.m_Degree;
    result.m_Variable = this->m_Variable;

    Polynomial reduced(*this);

    double GuidingFactor = Poly.m_Terms[Poly.size() - 1].Factor();


    while (reduced.m_Degree >= Poly.m_Degree)
    {
        int exp = reduced.m_Degree - Poly.m_Degree;
        double termFactor = reduced.m_Terms[reduced.size() - 1].Factor() / GuidingFactor;

        //I get an error on this next line. The compiler doesn't seem to find to right overloaded function for Poly_Mult
        reduced.Poly_Subtract(Poly_Mult(Poly, Polynomial(termFactor, Poly.m_Variable, exp)));

        result.m_Terms.push_back(VarPower(termFactor, exp));

        reduced.Simplify();
    }

    result.Simplify();

    *this = result;

    if(reduced.size() == 1 && reduced.m_Terms[0].Factor() == 0 && reduced.m_Terms[0].Exponent() == 0)
        return true;
    else
        return false;
}

The code is functionally the same in the friend function and it works perfectly as intended, no errors.

instead of the whole detailed function since the friend function works fine, but again, the compiler doesn't seem to find to right overloaded function. I really don't see the problem here, as the functions have different parameters so I see no ambiguity.

By simplifying the code, I get the same problem with this :

in my header file, I have my class definition :

class Polynomial
{
private:
    int m_Degree;
public:
    Polynomial(int degree)
        : m_Degree(degree) {};

    Polynomial(const Polynomial& Poly)
        : m_Degree(Poly.m_Degree) {};

    Polynomial& operator=(const Polynomial& Poly)
    {
        m_Degree = Poly.m_Degree;
    }


    void Multiply(const int& number);
    friend Polynomial Multiply(const Polynomial& Object, const int& number);

    void Divide(const int& number);
    friend Polynomial Divide(const Polynomial& Object, const int& number);
};

Then, in my cpp file, I have the implementation of the functions :

#include "Polynomial.h"

void Polynomial::Multiply(const int& number)
{
    m_Degree *= number;
}

Polynomial Multiply(const Polynomial& Object, const int& number)
{
    return Polynomial(Object.m_Degree * number);
}

void Polynomial::Divide(const int& number)
{
    Polynomial copy = *this;
    Polynomial result = Multiply(copy, (1./number));  //This is where the problem is. The compiler doesn't find the right overloaded function

    *this = result;
}

Polynomial Divide(const Polynomial& Object, const int& number)
{
    Polynomial result = Multiply(Object, (1. / number));

    return result;
}

I get those errors :

error C2660: 'Polynomial::Multiply': function does not take 2 arguments

no suitable constructor exists to convert from "void" to "Polynomial"

Julat
  • 1
  • 1
  • 4
    Can you make a [mre]? Remove all the code that is irrelevant, and add in necessary code so we can reproduce the error. Also, it would be helpful if you add the list of candidates the compiler found. – cigien Aug 31 '20 at 22:09
  • I edited my post to add a minimal reproducible example. Thank you for your help! – Julat Aug 31 '20 at 22:51
  • @Julat See [C++ friend function hidden by class function?](https://stackoverflow.com/questions/18449855/c-friend-function-hidden-by-class-function). – dxiv Aug 31 '20 at 22:53
  • @dxiv Yes, it works perfectly fine now! Thank you! I don't fully understand what was happening but the solution works – Julat Aug 31 '20 at 23:08

0 Answers0