我正在为多项式编写一个类。我在运算符重载时遇到错误。我是第一次使用它,所以错误可能是愚蠢的
get_size()
返回数组的最大长度。
coefficient[]
是coeff的数组。
exponant[]
代表指数
我已经使用参考声明了它们
int* coefficient;
int* exponent;
Polynomial.h
friend std::ostream& operator << (std::ostream&,Polynomial&);
Polynomial.cpp
//overloading << operator
std::ostream& operator << (std::ostream& out,Polynomial& obj)
{
for (int i=0 ; i < obj.get_size() ; i++)
{
if(obj.coefficient[i]!=0)
{
if (i== obj.get_size()-1) //last term does not have '+'
out << obj.coefficient[i] << "x^" << obj.exponent[i] <<endl;
else
std::ostream& i = out << obj.coefficient[i] << "x^" << obj.exponent[i] << " + ";
}
}
return out;
}
的main.cpp
cout << pol // where pol is object
提前致谢
答案 0 :(得分:2)
不知道错误是什么,这绝对不正确:
std::ostream& i = out << obj.coefficient[i] << "x^" << obj.exponent[i] << " + ";
应该是:
out << obj.coefficient[i] << "x^" << obj.exponent[i] << " + ";