c – 如何解决运算符>>重载错误(不匹配’运算符>>’
发布时间:2020-12-16 09:22:14 所属栏目:百科 来源:网络整理
导读:我已经查看了有关此问题的其他主题,并试图查看是否可以找到我的错误,但我无法找到如何解决我的错误. 我的错误: no match for ‘operator’ (operand types are ‘std::istream {aka std::basic_istreamchar}’ and ‘Polynomial()’)cin p3; 主要: // incl
|
我已经查看了有关此问题的其他主题,并试图查看是否可以找到我的错误,但我无法找到如何解决我的错误.
我的错误: 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;
}
解决方法
多项式p3();是函数声明,而不是变量定义(如您所料).它声明了一个名为p3的函数,它返回Polynomial并且没有参数.还要注意错误信息,它表示操作数类型是Polynomial(),这是一个函数.
将其更改为 Polynomial p3; 要么 Polynomial p3{}; // since C++11
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
