Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve operator>> overloading error (no match for 'operator>>')

I've looked through other topics about this and tried to see if I can find my error, but I wasn't able to find out how to solve my error.

My error:

no match for ‘operator>>’ (operand types are ‘std::istream {aka std::basic_istream<char>}’ and ‘Polynomial()’)
cin >> p3;

main:

// includes, namespace, etc...

int main()
{
    Polynomial p3();

    // Prompts user and assigns degrees and coefficients
    cout << "Enter the degree followed by the coefficients: ";
    cin >> p3;

    // other coding
 }

Header file definition for operator >>:

class Polynomial 
{
      private:
          double *coefs;
          int degree;
      public:
          // constructors, setters/getters, functions
          friend std::istream &operator >>(std::istream &in, Polynomial &poly);
};

Implementation file:

Polynomial::Polynomial() // default constructor
{
    degree = 0;
    coefs = new double[1];
    coefs[0] = 0.0;
}

std::istream &operator >>(std::istream &in, Polynomial &poly) ////!!!!!!
{
    in >> poly.degree;

    delete[] poly.coefs; // deallocate memory
    poly.coefs = new double[poly.degree + 1]; // create new coefficient array

    for(int i = 0; i <= poly.degree; i++) // assigns values into array
    {
        in >> poly.coefs[i];
    }

    return in;
}
like image 413
Amai Avatar asked Dec 21 '25 22:12

Amai


1 Answers

Polynomial p3(); is a function declaration, not a variable definition (as you expected). It declares a function named p3, which returns Polynomial and has no parameters. Also note the error message, it says operand type is Polynomial(), which is a function.

Change it to

Polynomial p3;

or

Polynomial p3{}; // since C++11
like image 188
songyuanyao Avatar answered Dec 24 '25 12:12

songyuanyao