【发布时间】:2016-06-03 23:01:18
【问题描述】:
我正在编写通过结构类从用户那里获取两组多项式的代码:
struct Term
{
double coefficient;
unsigned exponent;
Term *next;
};
然后提示要求他们对多项式进行加、减或求值(要求用户输入“x”的值)。我在编写加法和减法函数时遇到了麻烦。到目前为止,我为 add 函数编写了这个,但我不确定如何返回新的多项式。根据我对错误消息的理解,我不能使用类型为TermPtr 的 += 运算符。我不确定如何解决这个问题。
TermPtr add(TermPtr p1, TermPtr p2)
{
TermPtr newPoly;
if (p1 -> exponent == p2 -> exponent)
newPoly += ((p1 -> coeff) + (p2 -> coeff));
return newPoly;
}
我收到以下错误:
In function 'Term* add(TermPtr, TermPtr)':
36:19: error: invalid operands of types 'TermPtr {aka Term*}' and 'double' to binary 'operator+'
36:19: error: in evaluation of 'operator+=(using TermPtr = struct Term* {aka struct Term*}, double)'
【问题讨论】:
-
您的签名需要两个术语指针,并且您使用术语指针和双精度值进行调用。您需要为 (TermPtr, double) 提供 operator+ 重载。
标签: c++ linked-list nodes