Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;运算符==链表中的重载_C++_Linked List_Operator Overloading - Fatal编程技术网

C++ c++;运算符==链表中的重载

C++ c++;运算符==链表中的重载,c++,linked-list,operator-overloading,C++,Linked List,Operator Overloading,我想知道为什么重载==函数不起作用。我对私人主管感到困惑。私人头目是否会是最后一个制作的链接列表的头目?所以,如果我将最后一个链表与输入的LinkedList进行比较,那么它会起作用吗 追加代码 void LL::append(string pName,string phone) { Node *newNode = new Node; newNode->name = pName; newNode->phoneNumber = phone; newNod

我想知道为什么重载==函数不起作用。我对私人主管感到困惑。私人头目是否会是最后一个制作的链接列表的头目?所以,如果我将最后一个链表与输入的LinkedList进行比较,那么它会起作用吗

追加代码

void LL::append(string pName,string phone)
{
    Node *newNode = new Node;
    newNode->name = pName;
    newNode->phoneNumber = phone;
    newNode->next = nullptr;

    if (head == nullptr)
    {
        head = newNode;
    }
    else
    {
        Node *nodePtr = head;
        while (nodePtr->next != nullptr)
        {
            nodePtr = nodePtr->next;
        }
        nodePtr->next = newNode;
    }
}
深拷贝代码

LL::LL(const LL& source)
{
    head = nullptr;
    Node *nodePtr = source.head;
    while(nodePtr)
    {
        append(nodePtr->name,nodePtr->phoneNumber);
        nodePtr = nodePtr->next;
    }
}
main.cpp

#include <iostream>
#include "node.h"
using namespace std;

int main()
{
    LL list;
    list.append("jack","2");
    list.append("jack2","1");
    list.append("jack3","3");
    list.append("jack4","4");
    list.insertatBegin("notjack","0");
    list.print();
    list.searchByName("jack");
    cout << "cloning------------------" <<endl;
    LL list2(list);
    //list.destroy();
    //list2.append("jack","223");
    list2.print();
    if(list == list2)
    {
        cout << "same" <<endl;
    }
    else
    {
        cout << "not same" <<endl;
    }

}
#包括
#包括“node.h”
使用名称空间std;
int main()
{
LL列表;
列表。附加(“杰克”、“2”);
列表。附加(“jack2”、“1”);
列表。附加(“jack3”、“3”);
列表。附加(“jack4”、“4”);
列表。插入标记(“notjack”,“0”);
list.print();
列表。按名称搜索(“杰克”);

cout我不知道复制构造函数的实现是什么,如果它工作正常,那么它应该工作

bool LL::operator==(const LL& L1) const{
if (&L1==this){
    return true;
}
else{
    Node* current = L1.head;
    Node* lhs = this->head;
    while(current != nullptr){
        if(current->name != lhs->name || current->phoneNumber != lhs->phoneNumber)
            return false;
        current = current->next;
        lhs = lhs->next;
    }
    return true;
}}

我不知道什么是你的副本构造函数的实现,如果它是工作良好,那么这应该工作

bool LL::operator==(const LL& L1) const{
if (&L1==this){
    return true;
}
else{
    Node* current = L1.head;
    Node* lhs = this->head;
    while(current != nullptr){
        if(current->name != lhs->name || current->phoneNumber != lhs->phoneNumber)
            return false;
        current = current->next;
        lhs = lhs->next;
    }
    return true;
}}


这取决于你的复制构造函数。你在做深度复制吗?如果两个链表指向相同的内存,它们是相等的吗?或者包含相同的数据值吗?首先,你只比较指针,我想这不是你想要做的。接下来,你应该在比较运算符中有一个循环,但只有一个if。想想这对我来说意味着什么ans表示两个列表在一分钟内相等。如果列表中有相同的项目,则两个列表是相等的,顺序相同。这意味着您的
操作符==
必须一次循环两个列表中的一个项目,检查一个列表中的项目是否与另一个列表中的相应项目相同。我相信您可以看到你写的ode不是这样的。nodePtr==nodePtr2只有在它们是相同内存中的相同节点时才是真的。因此它们必须是相同的列表,并且你已经知道列表等于它本身。这取决于你的复制构造函数。你在做深度复制吗?如果两个链接列表指向相同内存,它们是相等的吗?或者包含相同的数据值?首先,你只比较指针,我想这不是你想要做的。接下来,你应该在比较运算符中有一个循环,但只有一个if。想想两个列表在一分钟内相等意味着什么。如果两个列表在列表上有相同的项,那么两个列表是相等的,顺序相同。这意味着您的
操作符==
必须一次循环两个列表中的一个项目,检查一个列表中的项目是否与另一个列表中的相应项目相同。我确信您可以看到您编写的代码与此完全不同。nodePtr==nodePtr2只有在它们是同一内存中的相同节点时才会为真。因此,它们将你必须是同一个列表,而且你已经知道一个列表等于它本身。你能解释一下“这个”是什么吗是引用。这是引用自身。这样我们就可以确保我们不是在比较相同的对象。我尝试运行了你的代码,但仍然没有得到预期的值。你能看看我的复制构造函数吗?我怎么知道你的append函数是如何工作的?你只增加了第一个节点指针,所以第二个节点指针就开始了e在比较时保持不变。因此结果将始终为假。如果您添加
lhs=lhs->next
,则它将工作。感谢您的帮助!您能解释一下“这”是什么吗是引用。这是引用自身。这样我们就可以确保我们不是在比较相同的对象。我尝试运行了你的代码,但仍然没有得到预期的值。你能看看我的复制构造函数吗?我怎么知道你的append函数是如何工作的?你只增加了第一个节点指针,所以第二个节点指针就开始了e在比较时保持不变。因此结果将始终为假。如果您添加
lhs=lhs->next
,则它将正常工作。感谢您的帮助!
bool LL::operator==(const LL& L1) const{
if (&L1==this){
    return true;
}
else{
    Node* current = L1.head;
    Node* lhs = this->head;
    while(current != nullptr){
        if(current->name != lhs->name || current->phoneNumber != lhs->phoneNumber)
            return false;
        current = current->next;
        lhs = lhs->next;
    }
    return true;
}}