Search code examples
c++cgccvirtual

Multiple class compile error


Still learning c++ and yet im stuck here, I got those classes:

Case.cpp:

#include "../inc_bomber/Case.hh"

Case::Case()
{

}

Case::~Case()
{

}

Case.hh:

#ifndef _CASE_HH_
# define _CASE_HH_

enum Type
  {
    FLOOR,
    WALL,
    BOMB,
    BLOCK,
    PLAYER1,
    PLAYER2,
    BOT,
    BONUS,
  };

class Case
{
private:

public:
  Case();
  virtual ~Case();

  virtual Type  getType() const = 0;
  virtual void  action() = 0;
  virtual int   getPos(int x, int y);
};

#endif /* CASE_HH_ */

I use those virtual on classes like this:

Player1.hh:

#ifndef _PLAYER1_HH_
# define _PLAYER1_HH_

#include "Case.hh"
#include "Map.hh"

class Player1 : public Case
{
private:
  int  x;
  int  y;

public:
  Player1();
  ~Player1();
  virtual Type getType() const;
  virtual void action();
  virtual void print_stuff();
  virtual int getPos(int x, int y);
};

#endif /* _PLAYER1_HH */

at the compile time I got this error, I guess im confusing the virtual usage but still dont find my mistake

g++ -Wall -Wextra -ansi -g3 -I includes/  -c src_bomber/Case.cpp -o src_bomber/Case.o
g++ -o bomber src_bomber/main.o src_bomber/GameMain.o src_bomber/Map.o src_bomber/Case.o src_bomber/Floor.o src_bomber/Block.o src_bomber/Wall.o src_bomber/Bomb.o src_bomber/Bot.o src_bomber/Player1.o src_bomber/Player2.o -Llibs/ -lgdl_gl -lGL -lGLEW -ldl -lrt -lfbxsdk -lSDL2 -lpthread -Wl,-rpath=./libs/
src_bomber/Case.o:(.rodata._ZTV4Case[vtable for Case]+0x30): undefined reference to `Case::getPos(int, int)'
collect2: ld returned 1 exit status
make: *** [bomber] Error 1

Solution

  • Case::getPos(int, int); is not marked as pure virtual (= 0), an so must have a supplied implementation (if you ever call getPos(int,int) on a reference to Case). You should add = 0 to the declaration of Case::getPos(int,int), as you did with all the other virtual member functions of Case, or you should add an implementation of Case::getPos(int,int), as you did with the constructor and destructor of Case.