C ++链接列表访问冲突

时间:2013-09-12 08:20:07

标签: c++ linked-list

我已经尝试过广泛搜索,但是所有相关的答案似乎都暗示在某个地方有一个空指针会造成麻烦。我已经多次检查过,有可能我只是公然地想念它。但是,由于错误将始终发生在node::node(const myObject& newmyObject)函数的末尾,无论我在myObject data = newmyObject;之后放置多少胡言乱语,并且在返回tail = head;之前,我真的不知所措。< / p>

这是第一年的编程任务,讲座和教科书都没有详细介绍涉及对象的链表,所以任何方向都会受到赞赏。

使用Visual Studio调试器时出现完全错误:First-chance exception at 0x00E38FFB in Assignment1.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC.

node.h

class node
{
public:
    node* next;
    myObject data();
    node();
    node(const myObject& newmyObject);
private:
};

node.cpp

node::node()
    {
        next = NULL;
    }

    node::node(const myObject& newmyObject)
    {
        next = NULL;
        myObject data = newmyObject;
    } // << crashes upon reaching the end of this, statements between newmyObject and here will execute fine

LinkedList.h

class LinkedList
{
    public:
        LinkedList();
        void addmyObject(myObject* newmyObject);
    private:
        int size;
        node* head;
        node* tail;
};

LinkedList.cpp

LinkedList::LinkedList()
{
    node* head = NULL;
    node* tail = NULL;
    myObject* tempmyObject;
}

void LinkedList::addmyObject(myObject* newmyObject)
{
    myObject * tempmyObject = newmyObject;
    if (head == NULL)
    {
        head = new node(*tempmyObject);
        tail = head;
    }
    else
    {
        node* newNode = new node(*tempmyObject);
        tail->next = newNode;
        tail = newNode;
    }
}

myObject.h

class myObject
{
    public:
        //constructor
        myObject ();
        myObject (std::string,int,int);

        //mutators
        void setnum1(int aTH);
        void setnum2(int aTM);
        //accessors
        int getnum1() const;
        int getnum2() const;

    private:
        std::string myObjectNumber;
        int num1;
        int num2;
};

myObject.cpp

//constructors
myObject::myObject(std::string fN, int aTH, int aTM)
{
    myObjectNumber = fN;
    num1 = aTH;
    num2 = aTM;
}
//mutators
void myObject::setnum1(int aTH)
{
    num1 = aTH;
}

void myObject::setnum2(int aTM)
{
    num2 = aTM;
}

//accessors
int myObject::getnum1() const
{
    return num1;
}

int myObject::getnum2() const
{
    return num2;
}

编辑 - 添加了myObject。对不起,如果可读性降低,我会匆忙地删除评论和内容。

0 个答案:

没有答案