如果有对象被创建为另一个类的数据成员,如何将值传递给参数化构造函数



我正试图使用OOPS概念在C++中制作一款国际象棋游戏,但遇到以下错误:

src/Game.cpp:6:36: error: no matching function for call to ‘Player::Player()’
Game::Game(): player1(1), player2(0){
^
In file included from include/Game.h:4:0,
from src/Game.cpp:2:

这是我的代码:

Player.h

#ifndef PLAYER_H
#define PLAYER_H
#include <string>
#include <King.h>
#include <Knight.h>
#include <Pawn.h>
#include <Queen.h>
#include <Bishop.h>
#include <Rook.h>
using namespace std;
class Player{
public:
Player(int color);
void getMove();   
int playerColor;             // 0 if player is black, 1 otherwise.   
private:
string move;                
// each player has the following pieces.
King king;                          
Queen queen;
Bishop bishop[2];
Rook rook[2];
Knight knight[2];
Pawn paws[8];
};
#endif 

播放器.cpp

#include "Game.h"
#include "Player.h"
#include <string>
#include <iostream>
using namespace std;

Player::Player(int color)
:playerColor(color){

}

Game.h

#ifndef GAME_H
#define GAME_H
#include <Pieces.h>
#include <Player.h>
class Game:public Player
{
public:
Game();
private:
Player player1;
Player player2;
Square cells[8][8];
bool gameStatus;
bool whiteTurn;
};

Game.cpp

#include <iostream>
#include "Game.h"
#include "Pieces.h"
using namespace std;
Game::Game(): player1(1), player2(0){

}

在Player.h文件中创建各种工件对象时,我也遇到了类似的错误如何创建这些对象?

错误的来源是Game源自Player

Game::Game(): player1(1), player2(0){
}

与相同

Game::Game(): Player(), player1(1), player2(0){
}

我的建议是不要使Player成为Game的基类。

// class Game : public Player
class Game
{
...
}

最新更新