jeudi 5 mai 2016

C++ How to access and change an object's member data in a map?

I inserted strings and Person objects in a map.

map <string,Person> _userList;

However, in my methods, when I try to add something in the Person class or change it's member data in the map, I can no longer access it. That's because when I use an iterator and test it by printing out i->second, the Person class that i is pointing to has BLANK information. All of the information of those Person objects are ERASED (or so I think). I don't really understand why. My goal is to print out the lists (member data) of the Person objects in the map. I can't do it when I have this problem because it'll print out BLANK.

Here is the code method that inserts strings and Person objects in the map:

void SocialNetwork::createPerson(string firstName, string lastName)
{
    string fullName = firstName + " " + lastName;
    //checks if the name is NOT A DUPLICATE
    //iterate through _userList. _userList is a map
    //if map is empty
    if (_userList.empty())
    {
        _user = new Person(firstName, lastName);
        _userList.insert(make_pair(fullName, *_user));
        _numUsers++;
    }
    else
    {
        bool matchFound = false;
        //USE MAP COUNT TO SEE IF THE PERSON ALREADY EXISTS IN THE MAP
        if(_userList.count(fullName)>0)
        {
            matchFound = true;
            cout << "Error: Name already exists" << endl;
        }
        else
        {
            _user = new Person(firstName, lastName);
            _userList.insert(make_pair(fullName, *_user));
            _numUsers++;
        }
    }
}

Here is sample code of a method that tries to print out a list in a Person class below:

void SocialNetwork::listPending(string personsFirst, string personsLast)
{
    //prints list of pending Friend Requests
    string personsFullName = personsFirst + " " + personsLast;
    //check if this user exists
    bool userExists = false;
    if (_userList.empty())
    {
        cout << "Error: Person does not exist" << endl;
    }
    else
    {
        if(_userList.count(personsFullName)>0)
        {
            userExists = true;
        }
        if (!userExists)
        {
            cout << "Error: Person does not exist" << endl;
        }
        else
        {
            map<string,Person>::iterator i = _userList.begin();
            bool personFound = false;
            while (!personFound)
            {
                if(i != _userList.end())
                {
                    if(i->first == personsFullName)
                    {
                        //PROBLEM IS HERE
                        personFound = true;
                        cout << i->second <<endl; //Test Code. Delete later. How come their Person class is left BLANK?
                        i->second.printPendingRequestList(); //How come the list is left BLANK?
                    }
                    i++;
                }
            }
        }
    }
}

And here is the printPendingRequestList method in the Person class:

void Person::printPendingRequestList()
{
    string test = _fullName;
    cout << _fullName << "'s pending list" << endl;
    queue<Person> toPrintQueue = _friendRequests;

    while (!toPrintQueue.empty())
    {
        cout << toPrintQueue.front().getFullName() << endl;
        toPrintQueue.pop();
    }
    cout << endl;
}

This is the output I received it's supposed to have the person's first name, last name, and all the other information but no, it is left default or blank:

First Name:
Last Name:
Full Name:
Number of Friends: 0
Number of people they blocked: 0
Friend Requests:


Friend List:

Block List:

Personal Message List:

's pending list

Please help

Aucun commentaire:

Enregistrer un commentaire