Я пишу стандартную игру «Броненосец», используя C++, с объектом «Игра», который содержит в себе два объекта Player. Когда я пытаюсь создать экземпляр объектов игрока в конструкторе игр, IntelliSense дает мне две ошибок:Создание объекта как атрибута класса с помощью настраиваемого конструктора (C++)
IntelliSense: выражение должно быть изменяемым Lvalue
IntelliSense: нет подходящего конструктора не существует для преобразования из «Игрока() "до "Игрок"
Вот мой файл заголовка:
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);
в
Player1 Player(Player1Name);
Player2 Player(Player2Name);
и ряд других вещей, но ничего не работает. Заранее большое спасибо!
'Player Player1();' является объявлением функции. –