错误:用构造函数中的指针重新定义多态类
我正在尝试在C ++中进行战舰游戏,并且我正在使用多态性的三个CPU级别进行编码,Targetet Board对象作为指针作为构造函数传递,直到我尝试制作一个驾驶的类和驾驶类和我一直收到此错误:
error: redefinition of 'cpu_medium::cpu_medium(board&)'
cpu_battleships.h的样本:
#ifndef CPU_BATTLESHIPS_H
#define CPU_BATTLESHIPS_H
#include "board.h"
class cpu_easy
{
public:
cpu_easy(board &e);
virtual ~cpu_easy();
protected:
board* enemy;
};
class cpu_medium : public cpu_easy
{
public:
cpu_medium(board &e):cpu_easy(e){};
};
#endif // CPU_BATTLESHIPS_H
cpu_battleships.cpp的样本
#include "cpu_battleships.h"
#include "board.h"
#include <cstdlib>
using namespace std;
cpu_easy::cpu_easy(board &e)
{
enemy=&e;
}
cpu_medium::cpu_medium(board &e):cpu_easy(e){}
{
}
i'm curretly trying to make a game of battleship in c++ and i am in the procces of coding three cpu levels with polymorphism,targetet board object is passed as a pointer into constructor and it works up until i try to make a drived class and i keep receiving this error :
error: redefinition of 'cpu_medium::cpu_medium(board&)'
sample of cpu_battleships.h:
#ifndef CPU_BATTLESHIPS_H
#define CPU_BATTLESHIPS_H
#include "board.h"
class cpu_easy
{
public:
cpu_easy(board &e);
virtual ~cpu_easy();
protected:
board* enemy;
};
class cpu_medium : public cpu_easy
{
public:
cpu_medium(board &e):cpu_easy(e){};
};
#endif // CPU_BATTLESHIPS_H
sample of cpu_battleships.cpp
#include "cpu_battleships.h"
#include "board.h"
#include <cstdlib>
using namespace std;
cpu_easy::cpu_easy(board &e)
{
enemy=&e;
}
cpu_medium::cpu_medium(board &e):cpu_easy(e){}
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您定义
cpu_medium :: cpu_medium(董事会&amp; e)
两次,正如错误消息所说。在cpu_battleships.h中,更改
为
cppu_battleships.cpp中的构造函数。
You define
cpu_medium::cpu_medium(board &e)
twice, exactly as the error message says.In cpu_battleships.h change
to
That way the constructor is only defined in cpu_battleships.cpp