0

当我重载 operator+ 时发生了一些奇怪的事情

我有一个单链表,我重载了 operator+ 和 operator=

这是我对两者的实现(编辑:使用 insertFront 和复制构造函数):

 AnyList::AnyList(const AnyList& list)
{
count = list.getCount();
first = list.getFirst();
AnyList a;
a.setFirst(first);
a.setCount(count);

Node *current = first;
for (int i = 0; i < count; ++i)
{
    a.insertFront(current->getData());
    current = current ->getLink();
}
}

-

      void AnyList::insertFront(int value)
{
Node *newNode = new Node;
newNode -> setData(value);
newNode -> setLink(first);
first = newNode;
++count;
}

AnyList AnyList::operator+ (const AnyList& list) const
{

Node *current = first;
Node *listCurrent = list.getFirst();

int sumHolder = 0;
AnyList temp;
while(current != NULL)
{
    sumHolder = current ->getData() + listCurrent ->getData();
    temp.insertFront(sumHolder);
    current = current ->getLink();
    listCurrent = listCurrent ->getLink();
}

return temp;
}


AnyList& AnyList::operator=(const AnyList& rightSide)
{

if(&rightSide != this)
{
    Node *travel = rightSide.getFirst();

    first = rightSide.getFirst();

    Node *original = first;

    while (travel != NULL)
    {
        original ->setData(travel ->getData());
        original ->setLink(travel ->getLink());
        travel = travel->getLink();
        original = original ->getLink();
    }
}

return *this;
}

以下是主要内容:

AnyList mylist;
AnyList mylist2;

for (int i = 0; i < 10; ++i)
{
    mylist.insertFront(i);
}
for (int i = 10; i < 20; ++i)
{
    mylist2.insertFront(i);
}

mylist.print();
cout << endl << endl;
mylist2.print();
cout << endl;
AnyList sumList = mylist + mylist2;
sumList.print();
cout << endl;

我的输出如下(这是我想要的输出):

    9 8 7 6 5 4 3 2 1 0 

19 18 17 16 15 14 13 12 11 10

10 12 14 16 18 20 22 24 26 28 

所以,我的问题是,当我改为写:

    AnyList sumList;
sumList = mylist + mylist2;
sumList.print();

当它进入打印函数并尝试从函数 getData() 返回数据时出现错误访问错误

我超级不知道为什么会这样,任何帮助将不胜感激!

谢谢!

4

4 回答 4

3

mylist + mylist2创建一个临时的,其析构函数在完整表达式的末尾被调用。您的赋值运算符执行浅拷贝,因此如果内存被调用的临时析构函数释放,sumList现在将具有悬空指针。

阅读如何进行深拷贝。

要对此进行测试,您可以暂时删除析构函数。只是为了测试 - 如果您正在管理资源,您将需要它。

于 2013-01-29T17:07:15.280 回答
1

这段代码

AnyList sumList = mylist + mylist2;

调用复制构造函数而不是operator=(请参阅复制初始化)

和这个

sumList = mylist + mylist2;

确实来电operator=

您的复制构造函数很可能会执行深层复制,这是正确的并且不会导致崩溃。你operator=做了浅拷贝,结果导致崩溃

于 2013-01-29T17:09:59.307 回答
0

Why do you get an error in one case and not the other? Because AnyList sumList = mylist + mylist2; does not call the = operator but instead invokes the copy constructor. Whereas

AnyList sumList;
sumList = mylist + mylist2;

calls the default constructor and then invokes the = operator.

You haven't shown us enough of your code to be sure why it is crashing when you use the operator= but I'd suggest that Luchian Grigore's answer probably has the right of it.

于 2013-01-29T17:10:58.700 回答
0

Your assignment operator copies the "first" pointer from the other list, so that both lists end up sharing the same nodes. (The loop in the operator is just a long-winded way of doing nothing, since you're assigning each node's data an pointer to themselves). Presumably, the list has a destructor which deletes its nodes, so you will end up with both lists trying to delete the same nodes; hence the error (or some other undefined behaviour).

If you want the list to be copyable and assignable at all, which is perhaps not a good idea, then you will have to do a "deep" copy, creating new nodes for the assigned-to list. Also, remember to delete the old nodes before reassigning your pointers to them.

Better still, unless you're learning how to implement a linked list, use the containers provided by the standard library.

于 2013-01-29T17:11:27.733 回答