Home > Net >  error: redefinition of polymorphic class with a pointer in constructor
error: redefinition of polymorphic class with a pointer in constructor

Time:06-07

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){}
{

}

CodePudding user response:

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

  • Related