小编hal*_*brd的帖子

使用自定义构造函数将对象实例化为类属性 (C++)

我正在使用 C++ 编写一个标准的战舰游戏,其中包含一个 Game 对象,其中包含两个 Player 对象。当我尝试在 Game 构造函数中实例化 Player 对象时,IntelliSense 给出两个错误:

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;
};
Run Code Online (Sandbox Code Playgroud)

我对 Player 构造函数的定义:

Player::Player(string name)
{
    SetName(name);
    //Initialize other variables that don't take input
{
Run Code Online (Sandbox Code Playgroud)

以及给出错误的代码:

//Game constructor
Game::Game(bool twoPlayer, string Player1Name, string Player2Name)
{
    Player1 …
Run Code Online (Sandbox Code Playgroud)

c++ constructor class object

3
推荐指数
1
解决办法
7078
查看次数

标签 统计

c++ ×1

class ×1

constructor ×1

object ×1