C++ 如何在地图中访问和更改对象的成员数据?
C++ How to access and change an object's member data in a map?
>我在映射中插入了字符串和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;
}
}
相关文章:
- 指向设备对象成员的指针
- 访问由unique_ptr传递的对象成员
- 引用对象成员函数的成员函数
- 将 C# 对象(包含静态对象成员)作为参数传递给 C++/CLI 程序
- Lua C API - 从C++分配和使用类的对象成员
- Rapidjson 遍历并获取复杂 JSON 对象成员的值
- 类对象成员变量在调用函数时不会更改
- 对象成员变量还是继承
- C++多态性:有没有办法找到对象成员函数的地址?
- 重载对象成员的比较运算符
- 获取未初始化对象成员的地址是否定义良好?
- 在优先级队列被推送到队列后,如何编辑对象成员
- 为什么在这里调用析构函数,以及在调用该对象析构函数后如何调用对象成员函数?
- 通过 std::bind 从对象成员检索值
- 保留对象成员变量的本地副本
- 有没有办法为静态对象成员定义一个符合开关标准的常量?
- 没有默认构造函数的对象成员的 wig setter
- 对象成员数组C++默认初始化
- 如何在数组中添加对象成员
- 使用聚合创建和关联两个不同的对象成员