C++ C++;超载<&书信电报;。错误:与';不匹配;操作员<<';(操作数类型为';std::ostream';{aka';std::basic#ostream<;char>;
C++;超载<&书信电报;。错误:与';不匹配;操作员<<';(操作数类型为';std::ostream';{aka';std::basic#ostream<;char>;';}和';Poly';)C++ C++;超载<&书信电报;。错误:与';不匹配;操作员<<';(操作数类型为';std::ostream';{aka';std::basic#ostream<;char>;,c++,C++,C++;超载<&书信电报;。错误:与';不匹配;操作员<<';(操作数类型为';std::ostream';{aka';std::basic#ostream<;char>;';}和';Poly';) 最近开始学习C++。现在我正试图超载,看起来你错过了一个声明。Addstd::ostream&operator很可能是您的代码缺少operator@NeilGatenby只有当您需要访问私人成员时,才
最近开始学习C++。现在我正试图超载,看起来你错过了一个声明。Add
std::ostream&operator很可能是您的代码缺少operator@NeilGatenby只有当您需要访问私人成员时,才需要使用friend
。在这种情况下,friend
声明也可以作为常规声明使用(即对世界说“某处有这样一个函数”),因此它解决了这个问题。但只有一份定期声明也应该有效。
class Poly {
public:
Poly(unsigned int n);
Poly(Poly &obj);
int& operator[] (int index);
unsigned int degree();
long eval(int x);
Poly& operator= (Poly& p);
private:
int *coeffs;
unsigned int n;
};`
std::ostream& operator << (std::ostream &os, Poly &p) {
for (int i = 0; i < p.degree()+1; i++){
if (i != 0)
os << " + " << p[i] << "x^" << i;
else
os << p[i];
}
os << std::endl;
return os;
}
int main() {
Poly *p1 = new Poly(2);
(*p1)[0] = 1;
(*p1)[1] = 1;
(*p1)[2] = 3;
cout<< p1->eval(2) << endl;
Poly p2(2);
p2 = (*p1);
cout << p2;
return 0;
}