重载的 Double Equal 运算符未按预期工作
Overloaded Double Equal operator is not working as expected
长长的双向链表代码。但问题是当我重载运算符时,我认为函数没有被调用。我试图在此处打印调试行,但从未出现过。
所以我想如果我的两个双向链表相等,它应该打印为真。
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;
}
对于初学者,运算符应使用限定符 const
声明
bool operator==(const Dlist &Q) const;
在此声明中
cout<<(x==y)<<endl;
所使用的表达式是两个指针 x
和 y
声明为
的比较
Dlist *x = new Dlist;
Dlist *y = new Dlist;
你需要比较尖的物体,比如
cout<<( *x == *y )<<endl;
注意这段代码存在逻辑错误
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;
代替语句
return true;
你应该写
return tempL1 == Trailer && tempL2 == Q.Trailer;
长长的双向链表代码。但问题是当我重载运算符时,我认为函数没有被调用。我试图在此处打印调试行,但从未出现过。 所以我想如果我的两个双向链表相等,它应该打印为真。
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;
}
对于初学者,运算符应使用限定符 const
声明bool operator==(const Dlist &Q) const;
在此声明中
cout<<(x==y)<<endl;
所使用的表达式是两个指针 x
和 y
声明为
Dlist *x = new Dlist;
Dlist *y = new Dlist;
你需要比较尖的物体,比如
cout<<( *x == *y )<<endl;
注意这段代码存在逻辑错误
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;
代替语句
return true;
你应该写
return tempL1 == Trailer && tempL2 == Q.Trailer;