简体   繁体   中英

C++: Linked Lists, Pointers to strings

I'm having trouble implementing a linked list, and returning the values of the list to the screen. Please excuse if this has been asked before - I couldn't find it, and perhaps I don't know how to ask the question correctly!

I am only testing out this code before I build it out for more functionality, but I can't seem to get off the ground. I've commented out some code here, for possible future use. I did not include the other methods below, only Display, particularly because there is nothing in them.

I ran cout statements to see that I am running into problems at statements like: person->lname. This will compile, but will not run. When I try to debug, I get an error withing the string header.

Any help is greatly appreciated!

      #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";      
    }
}

You need to initialize "person".

You have the definition "PeopleInfo* person" inside the "AllNames" class, and this means that person is a pointer to memory of type "PeopleInfo".

The thing is that it doesn't actually point to anything. You are only saying that it could point to such a class.

As "person" is null or garbage (you didn't make it anything), trying to assign something to person->lname will cause the program to crash. If you try to debug the contents of person->lname, that would also crash.

To solve the problem:

Before the first line of the AllNames constructor (right before "person->lname = "last";") put the command:

person = new PeopleInfo();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM