C++ 如何在地图中访问和更改对象的成员数据?

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

本文关键字:对象 成员 数据 地图 访问 C++      更新时间:2023-10-16

>我在映射中插入了字符串和Person对象。

map <string,Person> _userList;

但是,在我的方法中,当我尝试在 Person 类中添加某些内容或更改它在映射中的成员数据时,我无法再访问它。那是因为当我使用迭代器并通过打印出来测试它时i->其次,我指向的 Person 类具有空白信息。这些 Person 对象的所有信息都被擦除了(或者我认为是这样(。我真的不明白为什么。我的目标是打印出映射中 Person 对象的列表(成员数据(。当我遇到这个问题时,我无法做到这一点,因为它会打印出空白。

下面是在映射中插入字符串和 Person 对象的代码方法:

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++;
        }
    }
}

下面是尝试在下面的 Person 类中打印出列表的方法的示例代码:

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++;
                }
            }
        }
    }
}

下面是 Person 类中的 printPendingRequestList 方法:

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

这是我收到的输出,它应该包含该人的名字、姓氏和所有其他信息,但不,它保留为默认值或空白:

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

请帮忙

你的整个createPerson函数写得很糟糕。首先,它会动态创建一个对象并立即失去指向它的指针 - 从而产生内存泄漏。其次,它复制了不必要的代码。这是一个更好的版本:

void SocialNetwork::createPerson(string firstName, string lastName)
{
    string fullName = firstName + " " + lastName;
    auto it = _userList.find(fullName);
    if (it != _userList.end() {
        _userList.emplace(fullName, firstName, lastName);
        _numUsers++;
    } else {
        cout << "Error: Name already exists" << endl;
    }
}