我查看了有关此的其他主题并尝试查看是否可以找到我的错误,但我无法找到解决我的错误的方法。
我的错误:
no match for ‘operator>>’ (operand types are ‘std::istream {aka std::basic_istream<char>}’ and ‘Polynomial()’)
cin >> p3;
主要的:
// 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
}
运算符 >> 的头文件定义:
class Polynomial
{
private:
double *coefs;
int degree;
public:
// constructors, setters/getters, functions
friend std::istream &operator >>(std::istream &in, Polynomial &poly);
};
实现文件:
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;
}