【发布时间】:2013-11-25 01:07:53
【问题描述】:
*注意:这是一个作业:我不想要一个解决方案,只是一些提示让我朝着正确的方向思考。
我创建了多项式项对象 (PolyTerm) 的链接列表。我应该为 + 和 - 编写运算符重载。我一直无法找到一种方法来完成超载。在进行研究时,我相信我设置链接列表的方式可能无法完成重载。我想我应该为节点制作一个结构并将节点放入一个类中。相反,我只是将它设置为一个类,现在我正在努力尝试用指针重载(据我所知,你不能这样做)。
我创建了一个附加成员函数,想知道是否有任何方法可以使用它来阻止 + 运算符重载?我一直在阅读的所有内容似乎都表明没有,但我只想进行最终验证。
这是我班级的标题:
class PolyTerm
{
public:
/************************** CLASS CONSTRUCTORS *********************************/
PolyTerm(); // Default constructor
PolyTerm(int constant); // Constant term constructor
PolyTerm(int newExp, int newCoeff); // Unlinked term constructor
PolyTerm(int newExp, int newCoeff,
PolyTerm* next, PolyTerm* prev); // Full constructor
PolyTerm(PolyTerm* original); // Copy Constructor
/*******************************************************************************/
/**************************** CLASS DESTRUCTOR *********************************/
~PolyTerm();
/************************** ACCESSOR FUNCTIONS *********************************/
int getCoeff() const; // Returns coefficient of this term
int getExp() const; // Returns the exponent of this term
PolyTerm* getNext() const; // Returns the address of the next term
PolyTerm* getPrev() const; // Returns the address of the previous term
/*******************************************************************************/
/*************************** MUTATOR FUNCTIONS *********************************/
void setCoeff(int newCoeff); // Sets the value of this term's coefficient
void setExp(int newExp); // Sets the value of this term's coefficient
void setNext(PolyTerm* newNext); // Sets the value of this term's next term
void setPrev(PolyTerm* newPrev); // Sets the value of this term's prev term
/*******************************************************************************/
/**************************** MEMBER FUNCTIONS *********************************/
int evalTerm(int value); // Evaluates the term for using 'value'
void printTerm(); // Prints this term. ex '4x^3'
void printPoly(); // Prints the whole polynomial
void insertTerm(PolyTerm* afterMe, int exp, int coeff); // Node insertion
PolyTerm* addTogether(PolyTerm* p2); // adds this and p2 together.
PolyTerm* subtractThis(PolyTerm* p2); // subtracts this - p2.
/*******************************************************************************/
/************************** OPERATOR OVERLOADS *********************************/
const PolyTerm& operator+(const PolyTerm &other) const; // <----Cant figure this out
protected:
/**************************** MEMBER VARIABLES *********************************/
int exp; // The exponent of this term
int coeff; // The coefficient of this term
PolyTerm *next; // The location of the next term
PolyTerm *prev; // The location of the previous term
/*******************************************************************************/
private:
};
类中的其他所有内容都正常工作。这里也是 AddTogether 函数定义。它运作良好,只是不完全符合任务的要求。
PolyTerm* PolyTerm::addTogether(PolyTerm* p2)
{
PolyTerm* bigHead; // This pointer has an Ego Problem.
PolyTerm* big;
PolyTerm* small;
int bigDegree, smallDegree;
// Sets the bigger degree and smaller degree polynomial.
if(this->getExp() >= p2->getExp())
{
big = new PolyTerm(this);
small = p2;
}
else
{
big = new PolyTerm(p2);
small = this;
}
//Assign a head pointer for big polynomial (resultant of sums)
bigHead = big;
bigDegree = big->getExp();
smallDegree = small->getExp();
// Step through the members of the big polynomial that don't
// don't have a corresponding term in small one.
for (int i = 0; i < bigDegree - smallDegree; i++)
{
big = big->getNext();
}
// For each term that they have in common, add the coefficients
// and create a new term.
for (int i = 0; i <= smallDegree; i++)
{
big->setCoeff(big->getCoeff() + small->getCoeff());
big = big->getNext();
small = small->getNext();
}
return bigHead;
}
我不能为这个类设置运算符重载是对的吗?
【问题讨论】:
-
“我认为我应该为节点制作一个结构并将节点放入一个类中”是正确的。
标签: c++ linked-list operator-overloading