【发布时间】:2014-03-07 09:10:14
【问题描述】:
我是 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:访问冲突读取位置 0xCDCCDCDE5。”
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”部分一无所知..
【问题讨论】:
-
您正在访问一个空指针。可能有帮助:ericlippert.com/2014/03/05/how-to-debug-small-programs/
-
您的 AddNode (a) 检测到 NULL 头,分配一个节点,泄漏其内存,然后返回一个神秘指针
current,该指针从未在函数范围内设置或声明,或者(b) 检测到非 NULL 头,走到链表的末尾,然后立即丢失刚刚获得的用于分配新节点的指针,错误地(重复地)取消引用其next,返回指向该新节点的指针但从不将其添加到列表中。即这段代码还没有开始战斗。 -
temp是否已经在其他地方动态分配,而您要做的就是将其链接到head指向的(可能为空的)链表的尾部?current来自哪里?这显然是一个函数,但current和head似乎都没有被声明或传入。发布 real 函数,带参数,和 记录任何全局/外部变量。这段代码中有很多错误,修复它们取决于这些信息。 -
不要从下划线开始你的变量/函数/任何名称:stackoverflow.com/questions/228783/…
标签: c++ string linked-list addressbook unhandled-exception