gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:08:43 25 4
gpt4 key购买 nike

我正在尝试使用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:
这是我的代码:
播放器
#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
Player.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
{
...
}

关于c++ - 如果有对象被创建为另一个类的数据成员,如何将值传递给参数化的构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64790492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com