我目前遇到了一些与我的类和成员函数有关的问题,特别是从setter函数中获取用户输入,然后从打印信息函数中显示该信息。我有一个单独的功能,允许用户输入有关该字符的信息,然后使用一个吸气功能来打印出用户输入的信息。当我第一次运行它时,我有一个关于需要使用指向成员函数的指针的错误,并且在我的打印信息函数中添加了Character :: GetCharacterName和其他字符。现在,当我通过主函数运行程序(我没有包含它,因为它只是调用所有函数),我的程序将运行,但所有值都设置为1,无论用户输入什么内容。我知道它与指针有关,所以任何帮助正确设置它,以便它返回用户输入的值将不胜感激。由于C++帮助在类中设置指针
Character.h file
class Character
{
public:
Character();
void SetCharacterName();
void SetCharacterType();
void SetCharacterLevel();
string GetCharacterName();
string GetCharacterType();
double GetCharacterLevel();
void PrintInfo();
private:
string CharacterName;
string CharacterType;
double CharacterLevel;
};
Character.cpp file
Character::Character()
{
CharacterLevel = 1.0;
}
void Character::SetCharacterName()
{
cout << "\nWhat is the character's name? ";
cin >> CharacterName;
}
void Character::SetCharacterType()
{
cout << "\nWhat is the character's type? ";
cin >> CharacterType;
}
void Character::SetCharacterLevel()
{
cout << "\nWhat is the character's level? ";
cin >> CharacterLevel;
}
string Character::GetCharacterName()
{
return CharacterName;
}
string Character::GetCharacterType()
{
return CharacterType;
}
double Character::GetCharacterLevel()
{
return CharacterLevel;
}
void Character::PrintInfo()
{
system("pause");
system("cls");
cout << "\nCharacter name is " << &Character::GetCharacterName << ".\n";
cout << "\nCharacter type is " << &Character::GetCharacterType << ".\n";
cout << "\nCharacter level is " << &Character::GetCharacterLevel << ".\n";
}
虽然FPK的答案是正确的,但不需要使用getter,因为'PrintInfo()'可以直接访问属性,因此您可以简单地使用'std :: cout << CharacterName << std :: endl'。 – tomse