Dev*_*ana 1 c++ oop constructor class object
I am trying to make a Chess Game in C++ using OOPS concepts but face the following error:
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:
Run Code Online (Sandbox Code Playgroud)
Here is my code:
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
Run Code Online (Sandbox Code Playgroud)
Player.cpp
#include "Game.h"
#include "Player.h"
#include <string>
#include <iostream>
using namespace std;
Player::Player(int color)
:playerColor(color){
}
Run Code Online (Sandbox Code Playgroud)
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;
};
Run Code Online (Sandbox Code Playgroud)
Game.cpp
#include <iostream>
#include "Game.h"
#include "Pieces.h"
using namespace std;
Game::Game(): player1(1), player2(0){
}
Run Code Online (Sandbox Code Playgroud)
I am also getting similar error while creating various piece objects in Player.h file How to create these objects?
The source of the error is that Game is derived from Player.
Game::Game(): player1(1), player2(0){
}
Run Code Online (Sandbox Code Playgroud)
is the same as
Game::Game(): Player(), player1(1), player2(0){
}
Run Code Online (Sandbox Code Playgroud)
My suggestion is to not make Player a base class of Game.
// class Game : public Player
class Game
{
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
68 次 |
| 最近记录: |