使用自定义构造函数(C+;+;)将对象实例化为类属性 我用C++写了一个战舰的标准游戏,里面有一个包含两个玩家对象的游戏对象。当我尝试在游戏构造函数中实例化玩家对象时,IntelliSense给了我两个错误:

使用自定义构造函数(C+;+;)将对象实例化为类属性 我用C++写了一个战舰的标准游戏,里面有一个包含两个玩家对象的游戏对象。当我尝试在游戏构造函数中实例化玩家对象时,IntelliSense给了我两个错误:,c++,class,object,constructor,C++,Class,Object,Constructor,IntelliSense:表达式必须是可修改的左值 IntelliSense:不存在合适的构造函数将“Player()”转换为“Player” 这是我的头文件: class Player { public: Player(string name); //More unrelated stuff (Get/Set methods and Attributes) }; class Game { public: Game(bool twoPlayer, string Pla

IntelliSense:表达式必须是可修改的左值

IntelliSense:不存在合适的构造函数将“Player()”转换为“Player”

这是我的头文件:

class Player {
public:
    Player(string name);
    //More unrelated stuff (Get/Set methods and Attributes)
};


class Game {
public:
    Game(bool twoPlayer, string Player1Name, string Player2Name);
    //Get and Set methods (not included)
    //Attributes:
    Player Player1();
    Player Player2();
    int turn;
};
我对播放器构造函数的定义:

Player::Player(string name)
{
    SetName(name);
    //Initialize other variables that don't take input
{
以及给出错误的代码:

//Game constructor
Game::Game(bool twoPlayer, string Player1Name, string Player2Name)
{
    Player1 = Player(Player1Name);  //These two lines give the first error
    Player2 = Player(Player2Name);
    turn = 1;
}

//Game class Gets
int Game::GetTurn() { return turn; }
Player Game::GetPlayer1() { return Player1; }  //These two lines give the second error
Player Game::GetPlayer2() { return Player2; }
我做错了什么?我试过换衣服

Player1 = Player(Player1Name);
Player2 = Player(Player2Name);


还有很多其他的事情,但都不管用。提前多谢

问题似乎出在头文件中的类游戏中:

class Game {
public:
    Game(bool twoPlayer, string Player1Name, string Player2Name);
    //Get and Set methods (not included)
    //Attributes:
    Player Player1;// 
    Player Player2;//  
    int turn;
}; 

声明成员时请删除括号,否则您将创建名为Player1和Player2的函数,这些函数不带参数

问题似乎出在头文件中的类游戏中:

class Game {
public:
    Game(bool twoPlayer, string Player1Name, string Player2Name);
    //Get and Set methods (not included)
    //Attributes:
    Player Player1;// 
    Player Player2;//  
    int turn;
}; 

声明成员时请删除括号,否则您将创建名为Player1和Player2的函数,这些函数不带参数

Player1
Player2
都是函数。我假设,您希望它们成为一个成员变量

将游戏定义更改为:

class Game
{
public:
    Game(bool twoPlayer, string Player1Name, string Player2Name);

    //Get and Set methods (not included)

    //Attributes:
    Player Player1;
    Player Player2;
    int turn;
};
并使用初始化列表初始化您的成员:

Game::Game(bool twoPlayer, string Player1Name, string Player2Name)
: Player1(Player1Name)
, Player2(Player2Name)
, turn(1)
{
}
阅读更多有关为什么要初始化成员的信息:

Game::Game(bool twoPlayer, string Player1Name, string Player2Name)
: Player1(Player1Name)
, Player2(Player2Name)
, turn(1)
{
}
现在,这两条线:

Player Game::GetPlayer1() { return Player1; }
Player Game::GetPlayer2() { return Player2; }

将不再生成任何错误。

Player1
Player2
是函数。我假设,您希望它们成为一个成员变量

将游戏定义更改为:

class Game
{
public:
    Game(bool twoPlayer, string Player1Name, string Player2Name);

    //Get and Set methods (not included)

    //Attributes:
    Player Player1;
    Player Player2;
    int turn;
};
并使用初始化列表初始化您的成员:

Game::Game(bool twoPlayer, string Player1Name, string Player2Name)
: Player1(Player1Name)
, Player2(Player2Name)
, turn(1)
{
}
阅读更多有关为什么要初始化成员的信息:

Game::Game(bool twoPlayer, string Player1Name, string Player2Name)
: Player1(Player1Name)
, Player2(Player2Name)
, turn(1)
{
}
现在,这两条线:

Player Game::GetPlayer1() { return Player1; }
Player Game::GetPlayer2() { return Player2; }

将不再生成任何错误。

Player Player1()是一个函数声明。
Player Player1()是一个函数声明。啊!就这样!谢谢!啊!就这样!谢谢!