首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >链表只记得最近添加的对象。

链表只记得最近添加的对象。
EN

Stack Overflow用户
提问于 2014-03-19 21:42:11
回答 1查看 94关注 0票数 1

我有一个任务,要求我将对象添加到链接列表中。所讨论的对象是形状。

我的问题是,我可以将对象添加到列表中,但是当我试图打印出来时,只有最后添加的对象才会被打印出来,其余的只是垃圾值。

我的代码如下所示:

Source.cpp:

代码语言:javascript
运行
复制
#include "ShapeList.h"
#include <iostream>

using namespace std;

int main()
{
    ShapeList list;
    list.add(Rectangle(0,0,2,5));
    list.print();
}

我不允许更改此代码。例如,我不允许发送指向新矩形的指针,我应该“深拷贝”它。(我希望我用对了这个词。)

我的ShapeList.h看起来如下:

代码语言:javascript
运行
复制
#ifndef SHAPELIST_H
#define SHAPELIST_H
#include "Shape.h"
#include "Rectangle.h"

class ShapeList
{
private:
    Shape *conductor; //this will point to each node as it traverses the list
    Shape *root; //the unchanging first node
public:
    ShapeList();
    void print();
    void add(const Shape &s);
};

#endif

标题看起来是:

代码语言:javascript
运行
复制
#include "ShapeList.h"
#include <iostream>
using namespace std;

ShapeList::ShapeList()
{
    cout << "ShapeList created" << endl;
    root = new Shape; //now root points to a node class
    root->next = 0; //the node root points to has its next  pointer set to equal a null pointer
    conductor = root; //the conductor points to the first node
}

void ShapeList::add(const Shape &s)
{
    cout << "Shapelist's add function called" << endl;
    conductor->next = new Shape; //creates node at the end of the list
    conductor = conductor->next; //goes to next node
    Shape *pShape = s.clone(); //get a pointer to s
    conductor->current = pShape; //points current to pShape point
    conductor->next = 0; //prevents loops from going out of bounds
}

void ShapeList::print()
{
    conductor = root; //the conductor points to the start of the linked list
    if(conductor != 0)
    {
        while(conductor->next != 0)
        {
            conductor = conductor->next;
            cout << conductor->current->width << endl;
        }
        //cout << conductor->current->width << endl;
    }
}

所有形状的克隆函数都被重载,在本例中是矩形的:

代码语言:javascript
运行
复制
Rectangle * Rectangle::clone() const
{
    cout << "Rectangle's clone function called" << endl;
    Rectangle copiedRect(this);
    Rectangle * pCopiedRect = &copiedRect;
    return pCopiedRect;
}

Rectangle::Rectangle(const Rectangle *ref)
{
    cout << "Rectangle's copy constructor called" << endl;
    this->x = ref->x;
    this->y = ref->y;
    this->width = ref->width;
    this->height = ref->height;
}

我知道这很难读,我很抱歉。如果不需要的话我可以把东西移走。如果您愿意的话,我也可以添加更多。

我读过Allain的教程*关于链接列表,以及其他几篇文章。如果有人有另一篇文章,或者诸如此类的话,我会全神贯注的。

  • http://www.cprogramming.com/tutorial/c/lesson15.html
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-19 21:58:52

Rectangle::clone()正在调用未定义的行为。您正在返回一个自动变量copiedRect的地址,一旦函数终止,它就会下降。

试试这个:

代码语言:javascript
运行
复制
Rectangle * Rectangle::clone() const
{
    cout << "Rectangle's clone function called" << endl;
    return new Rectangle(*this);
}

而且你的复制机甚至不应该被实现。Rectangle的所有成员都是微不足道的可复制的。缺省值应该可以正常工作。

注意:我并没有花时间剖析您的列表插入代码,但是上面的问题肯定是需要解决的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22518716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档