简体   繁体   English

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

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

I'm currently having some problems with my class and the member functions, particularly with taking the users input from the setter functions and then displaying that info from the print info function. 目前,我的班级和成员函数存在一些问题,特别是从设置器函数获取用户输入,然后从打印信息函数显示该信息时。 I have a separate functions that allow the user to enter information about the character and then a getter function to then use in printing out the info that the user entered. 我有一个单独的功能,该功能允许用户输入有关字符的信息,然后再使用getter函数来打印出用户输入的信息。 When I first ran it I had an error about needing to use a pointer to a member function and I added the &Character::GetCharacterName and the others in my print info function. 第一次运行它时,我遇到一个错误,需要使用指向成员函数的指针,并在我的打印信息函数中添加了&Character :: GetCharacterName和其他字符。 Now when I run the program through my main function (I didn't include it because it simply calls all the functions) my program will run but all the values are set at 1 no matter what the user entered. 现在,当我通过主函数运行程序时(我没有包括它,因为它只是调用了所有函数),我的程序将运行,但是无论用户输入什么,所有值都设置为1。 I know it has something to do with pointers so any help with correctly setting this up so that it returns the values that the user entered would be appreciated. 我知道它与指针有关,因此任何有关正确设置此指针的帮助都将受到赞赏,以便它返回用户输入的值。 Thanks 谢谢

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";
}

Use () , to do a method call in PrintInfo : 使用()PrintInfo进行方法调用:

cout << "\nCharacter name is " << GetCharacterName() << ".\n";

etc. 等等

I sugest : this->GetCharacterName(); 我建议:this-> GetCharacterName(); In the print method. 在打印方法上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM