简体   繁体   English

声明一个 C++ 类

[英]Declaring a Class C++

I am struggling knowing how to create a class.我正在努力知道如何创建一个类。 I want to create a "Player" class and all I want to do is pass in the name while I'll have the other variables start at 0 until they are updated when a game is run (later in the program)我想创建一个“玩家”类,我想要做的就是传入名称,而我会让其他变量从 0 开始,直到它们在游戏运行时更新(稍后在程序中)

Player::Player(string name_in)    
{
    name = name_in;
    int numOfWins = 0;
    int numOfLoses = 0;
    int numOfDraws = 0;
    int totalMatches = 0;
}

Right now there are lots of errors around numOfWins, numOfLoses, numOfDraws and totalMatches.现在有很多关于 numOfWins、numOfLoses、numOfDraws 和 totalMatches 的错误。 What can I do to fix this?我能做些什么来解决这个问题?

Perhaps the error is in your int ... part of assignments, which essentially creates a new local variable in a constructor.也许错误出在你的int ...部分赋值中,它本质上在构造函数中创建了一个新的局部变量。

Try this version:试试这个版本:

#include <string>
using namespace std;

class Player
{
    string name;
    int numOfWins;
    int numOfLoses;
    int numOfDraws;
    int totalMatches;

public:
    Player(string name_in)    
    {
        name = name_in;
        numOfWins = 0;
        numOfLoses = 0;
        numOfDraws = 0;
        totalMatches = 0;
    }
};

The errors you get, at least from the snippet you posted are caused for you can't declare variables in constructor - you declare them in class body and initialize in constructor or using another function.你得到的错误,至少从你发布的片段中得到是因为你不能在构造函数中声明变量 - 你在类体中声明它们并在构造函数中初始化或使用另一个函数。

#include <string>

class Player {
public:
    Player( std::string const& name_in) : name( name_in),
                                          numOfWins(), numOfLoses(),
                                          numOfDraws(), totalMatches()
                                          {}  // constructor 
                                              // will initialize variables
                                              // numOfWins() means default 
                                              // initialization of an integer
private:
    std::string name;
    int numOfWins;
    int numOfLoses;
    int numOfDraws;
    int totalMatches;
};

usage:用法:

int main() {
    Player( "player_one");
    return 0;
}

You should declare other instance variables in the class declaration, rather than declaring them as locals (which is completely useless).您应该在类声明中声明其他实例变量,而不是将它们声明为局部变量(这是完全没用的)。

// This part goes in the header
class Player {
    string name;
    int numOfWins;
    int numOfLoses;
    int numOfDraws;
    int totalMatches;
public:
    Player(string name_in);
};

Now in the constructor you could use initialization lists:现在在构造函数中,您可以使用初始化列表:

// This part goes into the CPP file
Player::Player(string name_in)
// Initialization list precedes the body of the constructor
: name(name_in), numOfWins(0), numOfLoses(0), numOfDraws(0), totalMatches(0) {
// In this case, the body of the constructor is empty;
// there are no local variable declarations here.
}

Kinda vague, but I'll take a crack at it.有点含糊,但我会尝试一下。 You Probably want:你可能想要:

class Player{
    string name;    
    int numOfWins;
    int numOfLosses;
    int numOfDraws;
    int totalMatches;
    Player(string name_in)
};

Player::Player(string name_in){
    name = name_in;
    numOfWins = 0;
    numOfLosses = 0;
    numOfDraws = 0;
    totalMatches = 0;
}

Haven't used C++ in a while, so this may be faulty.有一段时间没有使用 C++,所以这可能是错误的。

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

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