C ++错误:0x00934ABB处的未处理异常(链表,地址簿)

时间:2014-03-07 09:10:14

标签: c++ string linked-list addressbook unhandled-exception

我是C ++的新手,我有一个关于创建这个地址簿程序的项目,我遇到了下面的代码问题。该代码应该创建记录。

cin >> ch;
switch(ch)
{
    case '1':
        system("cls");
        cout << "\n\nINSERT RECORD";
                        cout << "\n\nInput FIRST NAME: ";
                        cin.ignore();
                        getline(cin,firstname,'\n');
                        cout << "\n\nInput LAST NAME: ";
                        getline(cin,lastname,'\n');
                        cout << "\n\nInput PHONE NUMBER: ";
                        getline(cin,phonenumber,'\n');
                        cout << "\n\nInput DAY OF BIRTH: ";
                        getline(cin,dayofbirth,'\n');
                        cout << "\n\nInput MONTH OF BIRTH: ";
                        getline(cin,monthofbirth,'\n');
                        cout << "\n\nInput YEAR OF BIRTH: ";
                        getline(cin,yearofbirth,'\n');
                        cout << "\n\nInput AGE: ";
                        getline(cin,age,'\n');
                        cout << "\n\nInput STREET NAME (Address): ";
                        getline(cin,streetname,'\n');
                        cout << "\n\nInput CITY (Address): ";
                        getline(cin,city,'\n');
                        cout << "\n\nInput STATE (Address): ";
                        getline(cin,state,'\n');
                        cout << "\n\nInput ZIP CODE (Address): ";
                        getline(cin,zipcode,'\n');
                        cout << "\nRecord inserted!";
                    current = ad.AddNode(temp);
            ad.userPromptStatement();
    break;


    node* AddressBook::AddNode(nodePtr temp)
    { 
        string firstname;
        string lastname;
        string phonenumber;
        string dayofbirth;
        string monthofbirth;
        string yearofbirth;
        string age;
        string streetname;
        string city;
        string state;
        string zipcode;
        AddressBook ad;

           if(head != NULL)
            {
                current = head;
                while(current -> next != NULL)
                {
                    current = current -> next;
                }
                current = new node;
                current -> next -> firstname = temp -> firstname;
                current -> next -> lastname = temp -> lastname;
                current -> next -> phonenumber = temp -> phonenumber;
                current -> next -> dayofbirth = temp -> dayofbirth;
                current -> next -> monthofbirth = temp -> monthofbirth;
                current -> next -> yearofbirth = temp -> yearofbirth;
                current -> next -> age = temp -> age;
                current -> next -> streetname = temp -> streetname;
                current -> next -> city = temp -> city;
                current -> next -> state = temp -> state;
                current -> next -> zipcode = temp -> zipcode;
                current -> next = nullptr;
                return current;
                ad.userPromptStatement();
            }
           else
            {
                head = new node;
                head -> firstname = temp -> firstname;
                head -> lastname = temp -> lastname;
                head -> phonenumber = temp -> phonenumber;
                head -> dayofbirth = temp -> dayofbirth;
                head -> monthofbirth = temp -> monthofbirth;
                head -> yearofbirth = temp -> yearofbirth;
                head -> age = temp -> age;
                head -> streetname = temp -> streetname;
                head -> city = temp -> city;
                head -> state = temp -> state;
                head -> zipcode = temp -> zipcode;
                head -> next = nullptr;
                return current;
            }
    }

我在“xstring”中一直收到错误。

当我创建1条记录时,错误不会发生,但是当我创建第二条记录时,我得到了这个: “dummy3.exe中0x00934ABB处的未处理异常:0xC0000005:访问冲突读取位置0xCDCDCDE5。”

bool _Grow(size_type _Newsize,
    bool _Trim = false)
    {   // ensure buffer is big enough, trim to size if _Trim is true
    if (max_size() < _Newsize)
        _Xlen();    // result too long
    if (this->_Myres < _Newsize) /*** <- next statement that will be executed points to this line ***/
        _Copy(_Newsize, this->_Mysize); // reallocate to grow
    else if (_Trim && _Newsize < this->_BUF_SIZE)
        _Tidy(true, // copy and deallocate if trimming to small string
            _Newsize < this->_Mysize ? _Newsize : this->_Mysize);
    else if (_Newsize == 0)
        _Eos(0);    // new size is zero, just null terminate
    return (0 < _Newsize);  // return true only if more work to do
    }

任何想法?我对“xstring”部分一无所知..

2 个答案:

答案 0 :(得分:1)

你的问题在这里:

current = new node;
current -> next -> firstname = temp -> firstname;

您刚刚创建了一个新节点(替换当前),然后尝试访问未初始化的子节点(下一个)。

这应该更好:

current -> next = new node;
current -> next -> firstname = temp -> firstname;
...
current -> next -> next = nullptr;
return current -> next;

另一个提示:如果动态创建tmp,那么考虑直接使用它而不是复制其元素。像这样:

current -> next = tmp;

我不知道你的所有代码,所以我不能说这是否适合你。

答案 1 :(得分:0)

该错误的原因可能是尝试访问空指针。

您应该在使用之前创建 temp 。我看不到任何名称是temp的指针。请先创建temp,然后使用malloc或memset将其分配给内存。然后你可以传递错误。