C++ 帮助在 类 中设置指针
C++ help setting up pointers in classes
我目前在使用 class 和成员函数时遇到了一些问题,尤其是在从 setter 函数获取用户输入然后从打印信息函数显示该信息时。我有一个单独的函数,允许用户输入有关字符的信息,然后使用 getter 函数打印出用户输入的信息。当我第一次 运行 时,我遇到了一个关于需要使用指向成员函数的指针的错误,我在打印信息函数中添加了 &Character::GetCharacterName 和其他函数。现在,当我 运行 程序通过我的主要功能时(我没有包含它,因为它只是调用所有功能)我的程序将 运行 但无论用户如何,所有值都设置为 1进入。我知道它与指针有关,所以任何帮助正确设置它以便 returns 用户输入的值将不胜感激。谢谢
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";
}
使用 ()
,在 PrintInfo
中执行方法调用:
cout << "\nCharacter name is " << GetCharacterName() << ".\n";
等等
我建议:
this->GetCharacterName();
在打印方法中。
我目前在使用 class 和成员函数时遇到了一些问题,尤其是在从 setter 函数获取用户输入然后从打印信息函数显示该信息时。我有一个单独的函数,允许用户输入有关字符的信息,然后使用 getter 函数打印出用户输入的信息。当我第一次 运行 时,我遇到了一个关于需要使用指向成员函数的指针的错误,我在打印信息函数中添加了 &Character::GetCharacterName 和其他函数。现在,当我 运行 程序通过我的主要功能时(我没有包含它,因为它只是调用所有功能)我的程序将 运行 但无论用户如何,所有值都设置为 1进入。我知道它与指针有关,所以任何帮助正确设置它以便 returns 用户输入的值将不胜感激。谢谢
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";
}
使用 ()
,在 PrintInfo
中执行方法调用:
cout << "\nCharacter name is " << GetCharacterName() << ".\n";
等等
我建议: this->GetCharacterName(); 在打印方法中。