我正在使用 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)