【发布时间】:2020-11-05 18:19:23
【问题描述】:
它是一个长的双向链表代码。但问题是当我重载运算符时,我认为该函数没有被调用。我曾尝试在此处打印调试行,但从未出现过。 所以我想如果我的两个双向链表相等,它应该打印为 true。
class Node
{
friend class Dlist;
private:
string s;
string language;
int noOfNode;
Node * Next;
Node * Prev;
};
class Dlist
{
private:
Node * Header;
Node * Trailer;
int n;
public:
Dlist();//default constructor
void AddFront(string e,string lang);
void Print();
void AddBack(string e,string lang);
void RemoveFront();
void RemoveBack();
int Empty(){ if (Header->Next==Trailer) return 1 ; else return 0;}
int CountLanguage(string lang);
int search (string r);
void RemoveWord(string tempW);
void changeIndex(Node* node,int newIndex);
void sortDLL();
void PrintRev();
void AddInOrder(string s, string language);
bool operator==(const Dlist &Q);
};
bool Dlist::operator ==(const Dlist &Q)
{
Node*tempL1=Header->Next;
Node *tempL2=Q.Header->Next;
int count=0;
cout<<"here"<<endl;
if(n==Q.n)
{
while(tempL1!=Trailer && tempL2!=Q.Trailer)
{
if(tempL1->s==tempL2->s && tempL1->language==tempL2->language
){
tempL1=tempL1->Next;
tempL2=tempL2->Next;
}
else return false;
}
return true;
}
else
return false;
}
int main()
{
Dlist *x = new Dlist;
Dlist *y = new Dlist;
Dlist *z = new Dlist;
inputX();
inputY();
cout<<endl<<"test if the first list and the second are equal?? :"<<endl;
cout<<(x==y)<<endl;
return 0;
}
【问题讨论】:
-
看看你的
operator ==。参数的类型是什么?是否匹配x和y的类型? -
应该是bool operator==(const Dlist *Q);?
-
这可能行得通,但是比较
x和y指向的对象不是更好吗? -
@cigien 比较对象是什么意思?实际上我通过遍历列表来检查每个节点
-
@AntarRoy 您不能为指针重载运算符。而是将
x、y和z设为非指针
标签: c++ class operator-overloading doubly-linked-list