错误:用构造函数中的指针重新定义多态类

发布于 2025-02-05 01:53:11 字数 840 浏览 2 评论 0原文

我正在尝试在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷迭香的记忆 2025-02-12 01:53:11

您定义cpu_medium :: cpu_medium(董事会&amp; e)两次,正如错误消息所说。

在cpu_battleships.h中,更改

cpu_medium(board &e):cpu_easy(e){};

cpu_medium(board &e);

cppu_battleships.cpp中的构造函数。

You define cpu_medium::cpu_medium(board &e) twice, exactly as the error message says.

In cpu_battleships.h change

cpu_medium(board &e):cpu_easy(e){};

to

cpu_medium(board &e);

That way the constructor is only defined in cpu_battleships.cpp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文