【发布时间】:2013-07-14 18:23:51
【问题描述】:
if (polynomial1->get(0)->compareTo(polynomial2->get(0)) == 0)
{
polynomial1->get(0)->coefficient += polynomial2->get(0)->coefficient;
result->insert_tail->polynomial1->get(0);
}
Polynomial1 和 Polynomial2 都是链接列表,我一次将多项式项添加到一个节点中。在我的 compareTo 函数中,如果链表中的两个项 == 0 那么我想访问系数并将两个项的系数相加。我的问题是访问系数。我不断收到错误消息:
类
Data没有名为‘coefficient’的成员
但是我的PolynomialTerm 类继承了Data。获取系数有什么帮助吗?
class PolynomialTerm : public Data
{
public:
int coefficient;
Variable *variable;
PolynomialTerm(int coefficient, Variable *variable) :
coefficient(coefficient), variable(variable)
{ }
int compareTo(Data *other) const
{
PolynomialTerm * otherTerm = (PolynomialTerm*)other;
return variable->variableX == otherTerm->variable->variableX &&
variable->variableX == otherTerm->variable->variableX &&
variable->exponentX == otherTerm->variable->exponentX &&
variable->exponentY == otherTerm->variable->exponentY ? 0 :
variable->exponentX > otherTerm->variable->exponentX ||
variable->exponentY > otherTerm->variable->exponentY ? -1 : 1;
}
---编辑--
这也是我的数据类,它位于我的头文件中。
class Data {
public:
virtual ~Data() {}
/**
* Returns 0 if equal to other, -1 if < other, 1 if > other
*/
virtual int compareTo(Data * other) const = 0;
/**
* Returns a string representation of the data
*/
virtual string toString() const = 0;
};
【问题讨论】:
-
请贴出
class Data的定义。
标签: c++ linked-list polynomial-math