如何将指向某个人配偶姓名的指针存储为该人类的私人成员?
例如,假设我有以下代码:
#include <iostream>
#include <list>
using namespace std;
class person
{
private:
string name;
string *spouse;
public:
void setName(string tempName) { name = tempName; }
void setSpouse(string &tempSpouse) { spouse = &tempSpouse; } // ERROR HERE?
string getName() { return name; }
string getSpouse() { return spouse; } // ERROR HERE?
};
int main()
{
person entry;
list<person> personList;
list<person>::iterator itr1, itr2;
/* Adding two people/nodes to the linked list. */
entry.setName("John Doe");
personList.push_back(entry);
entry.setName("Tina Doe");
personList.push_back(entry);
/* Attempting to assign Tina Doe as John Doe's spouse. */
for (itr1 = personList.begin(); itr1 != personList.end(); itr1++)
{
if (itr1->getName() == "John Doe")
{
for (itr2 = personList.begin(); itr2 != personList.end(); itr2++)
{
if (itr2->getName() == "Tina Doe")
{
itr1->setSpouse(itr2->getName()); // ERROR HERE?
}
}
}
}
/* Displaying all Names with Spouses afterwards. */
for (itr1 = personList.begin(); itr1 != personList.end(); itr1++)
{
cout << "Name: " << itr1->getName() << " | Spouse: " << itr1->getSpouse() << endl;
}
return 0;
}
我无法将配偶名称的地址分配给班级中的指针成员。我在评论中注意到我认为可能存在错误。
您可以在此处查看代码和错误:https://ideone.com/4CXFnt
任何帮助将不胜感激。谢谢。
答案 0 :(得分:0)
getName
返回一个临时的std::string
(name
变量的副本),编译器正在尝试使您免于引用即将删除的内存部分。此错误与列表无关 - 要修复它,您需要将副本存储在spouse
变量中(这将导致在多个位置存储相同的数据)或在getName
中返回引用。你也可以考虑创建另一个访问者(私有访问者),但它很难看。
我建议存储副本,但如果确实需要引用/指针,那么修改行就足够了:
string getName() { return name; }
string getSpouse() { return spouse; } // ERROR HERE?
到
string& getName() { return name; }
string getSpouse() { return *spouse; } // ERROR HERE?
然而,为了保持一致性,我建议:
string& getName() { return name; }
string& getSpouse() { return *spouse; } // ERROR HERE?
答案 1 :(得分:0)
你有两个问题。第一个很容易解决:getSpouse
没有返回正确的类型。你应该写
string getSpouse() { return *spouse; } // Derefencing the pointer and returning a string
// or
string* getSpouse() { return spouse; } // Returning the pointer
第二个问题更微妙。写下:itr2->getName()
时,您只有一个值(itr2->名称的副本):您不存储可以更改的变量,因此您不能引用它。
没有简单的方法可以在类实例中指向另一个实例的私有变量。我猜你应该质疑你做事的方式并创建一个指向person
而不是名字的指针(记住指针很便宜,它们只是内存位置)。
class person
{
private:
string name;
person *spouse;
public:
void setSpouse(person *_spouse) { spouse = _spouse; }
person* getSpouse() { return spouse; }
string getSpouseName() { return spouse->getName(); }
};
这项工作,但仍然不安全:如果spouse
被销毁并尝试从person
访问它,您将遇到深层问题...(请注意,您遇到了同样的问题string
)。
那么修复是什么?好吧,如果您只希望B
成为A
的配偶,您可以先创建B
并使用构造函数在A
中进行引用。但是,如果您希望B
成为A
的配偶A
和B
的配偶,您必须使用上述不安全技巧并小心,或者存储配偶&#39;在课外列出。