gpt4 book ai didi

C++:链表、字符串指针

转载 作者:行者123 更新时间:2023-11-30 05:34:09 25 4
gpt4 key购买 nike

我在实现链表以及将列表的值返回到屏幕时遇到问题。如果之前有人问过这个问题,请原谅 - 我找不到它,也许我不知道如何正确地提出这个问题!

我只是在构建它以获得更多功能之前测试这段代码,但我似乎无法开始。我在这里注释掉了一些代码,以备将来使用。我没有包括下面的其他方法,只有 Display,特别是因为它们什么都没有。

我运行了 cout 语句以查看我在以下语句中遇到了问题:person->lname。这将编译,但不会运行。当我尝试调试时,出现字符串 header 错误。

非常感谢任何帮助!

      #include <iostream>
#include <string>
using namespace std;

struct Birthday
{
int month;
int day;
int year;
};

struct Anniversary
{
int month;
int day;
int year;
};

struct Address
{
string street;
string city;
string state;
int zip;
};

struct PeopleInfo
{
string lname;
string fname;
Address fulladdr;
Birthday bday;
Anniversary aday;
//PeopleInfo* nextperson;
};

class AddressBook
{
public:
AddressBook();
AddressBook(string);
void NewPerson();
void DeletePerson();
void SetName();
void SetAddress();
void SetDates();
void Search(string);
void Sort();
void Display();
void BDayCard();
void ADayCard();
private:
PeopleInfo* person;
//PeopleInfo* currentperson;
int length;
};

AddressBook::AddressBook()
{
person->lname = "last";
person->fname = "first";
person->fulladdr.street = "default st.";
person->fulladdr.city = "anytown";
person->fulladdr.state = "NJ";
person->fulladdr.zip = 00000;
person->bday.month = 0;
person->bday.day = 0;
person->bday.year = 0;
person->aday.month = 0;
person->aday.day = 0;
person->aday.year = 0;
length = 0;
//person->nextperson->lname = NULL;

//currentperson->nextperson->lname = NULL;
}
void AddressBook::Display()
{
cout << person->lname << ", " << person->fname << " " << endl
<< person->fulladdr.street << endl
<< person->fulladdr.city << ", " << person->fulladdr.state << " " << person->fulladdr.zip << endl
<< "Birthday: " << person->bday.month << "/" << person->bday.day << "/" << person->bday.year << endl
<< "Anniversary: " << person->aday.month << "/" << person->aday.day << "/" << person->aday.year << endl << endl;
}

// Function Prototypes
void Menu(char &entry);
void Action(char &entry, AddressBook AllNames);

// Main
int main()
{
cout << "test\n";
char entry;
cout << "test\n";
AddressBook AllNames;
cout << "test\n";
do
{
Menu(entry);
Action(entry, AllNames);
} while (!(entry == 'E' || entry == 'e'));
return 0;
}

void Menu(char &entry)
{
cout << "\nADDRESSBOOK\n\n"
<< "(N) Enter New Name\n"
<< "(D) Delete a Name\n"
<< "(C) Change a Name/Date\n"
<< "(U) Update Anniversary/Birthday\n"
<< "(S) Show Address Book Entries\n"
<< "(B) Make a Birthday Card\n"
<< "(A) Make an Anniversary Card\n"
<< "(E) Exit Program\n\n";
cin >> entry;
cout << "\n";
}

void Action(char &entry, AddressBook AllNames)
{
switch (entry)
{
case 'N':
case 'n': AllNames.NewPerson(); break;
case 'D':
case 'd': AllNames.DeletePerson(); break;
case 'C':
case 'c': AllNames.SetName(); break;
case 'U':
case 'u': AllNames.SetDates(); break;
case 'S':
case 's': AllNames.Display(); break;
case 'B':
case 'b': AllNames.BDayCard(); break;
case 'A':
case 'a': AllNames.ADayCard(); break;
case 'E':
case 'e': break;
default: "\nPlease input a valid entry\n";
}
}

最佳答案

你需要初始化“person”。

您在“AllNames”类中定义了“PeopleInfo* person”,这意味着 person 是指向“PeopleInfo”类型内存的指针。

问题是它实际上并没有指向任何东西。你只是说它可以指向这样一个类。

由于“person”为 null 或垃圾(您什么都没做),尝试将某些内容分配给 person->lname 将导致程序崩溃。如果您尝试调试 person->lname 的内容,那也会崩溃。

解决问题:

在 AllNames 构造函数的第一行之前(右边 before "person->lname = "last";")放置命令:

person = new PeopleInfo();

关于C++:链表、字符串指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34375523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com