簡體   English   中英

C ++幫助在類中設置指針

[英]C++ help setting up pointers in classes

目前,我的班級和成員函數存在一些問題,特別是從設置器函數獲取用戶輸入,然后從打印信息函數顯示該信息時。 我有一個單獨的功能,該功能允許用戶輸入有關字符的信息,然后再使用getter函數來打印出用戶輸入的信息。 第一次運行它時,我遇到一個錯誤,需要使用指向成員函數的指針,並在我的打印信息函數中添加了&Character :: GetCharacterName和其他字符。 現在,當我通過主函數運行程序時(我沒有包括它,因為它只是調用了所有函數),我的程序將運行,但是無論用戶輸入什么,所有值都設置為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";

等等

我建議:this-> GetCharacterName(); 在打印方法上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM