c++帮助在类中设置指针
C++ help setting up pointers in classes
我目前对我的类和成员函数有一些问题,特别是从setter函数中获取用户输入,然后从打印信息函数中显示该信息。我有一个单独的函数,允许用户输入有关字符的信息,然后是一个getter函数,然后用于打印用户输入的信息。当我第一次运行它时,我有一个关于需要使用指针到成员函数的错误,我在我的打印信息函数中添加了&Character::GetCharacterName和其他。现在,当我通过main函数运行程序时(我没有包含它,因为它只是调用所有函数),我的程序将运行,但是无论用户输入什么,所有的值都被设置为1。我知道它与指针有关,所以任何帮助正确设置它,以便它返回用户输入的值将是感激的。由于
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";
等。
我建议:——> GetCharacterName ();
相关文章:
- 将指针设置为"nullptr"并不能防止双重删除?
- 聚合初始化,将成员指针设置为同一结构成员
- 从 Swift 设置C++指针
- 是否可以使用智能指针成员设置具有另一个结构的结构?
- 尽管直接设置了指针数组,但仍为空
- 如何设置指针的内存地址
- QTableView并设置指针位置
- C++:根据模板参数设置指针的常量
- 从c++函数中设置指针变量
- 复制设置指针为空
- 正确设置指针成员变量
- c++函数返回与函数设置指针
- 在宿主结构体上设置指针时出现意外结果
- 显式设置指针的值
- 在创建单链表时正确设置指针的问题
- 在c++中设置指针到指针数据
- 如何设置指针的push_back格式
- 设置指针为空
- 为什么不能在函数中设置指针
- c++帮助在类中设置指针