3

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;
}
Amai
  • 141
  • 6
  • Possible duplicate of [My attempt at value initialization is interpreted as a function declaration, and why doesn't A a(()); solve it?](https://stackoverflow.com/questions/1424510/my-attempt-at-value-initialization-is-interpreted-as-a-function-declaration-and) – smac89 Nov 19 '18 at 02:37
  • Aka the most vexing parse – smac89 Nov 19 '18 at 02:39

1 Answers1

6

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
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • Thank you so much! I can't believe a simple error gave me so much problems. Been looking at this problem for over an hour before seeking help. Really appreciate it! Will accept answer after the time is up! – Amai Nov 19 '18 at 02:38