c++帮助在类中设置指针

C++ help setting up pointers in classes

本文关键字:设置 指针 帮助 c++      更新时间:2023-10-16

我目前对我的类和成员函数有一些问题,特别是从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 ();