Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Builder error from abstract class undefined reference to `vtable for [duplicate]

Possible Duplicate:
Undefined symbols “vtable for …” and “typeinfo for…”?
C++ Undefined Reference to vtable and inheritance

I've a problem with a little project required from my university. It's a simple project of a chess game.

I've the obscure error undefined reference to `vtable for XXX when I define an inherited class from an abstract one... This is the code

Pieces.h

class Pieces {
public:
   Pieces(char color) : pieceColor(color) {}
   virtual ~Pieces() {}
   virtual char getPieceType() = 0;
   char getColor() {
     return pieceColor;
   }

   virtual bool isLegalMove(int srcRow, int srcCol, int destRow, int destCol, Pieces* board[8][8]) = 0;

private:
   virtual bool areSquaresLegal(int srcRow, int srcCol, int destRow, int destCol, Pieces* board[8][8]) = 0;
   char pieceColor;
};

and this a sample inherited class i.e. the pawn one

Pawn.h

#include "Pieces.h"

class Pawn: public Pieces {
public:
    Pawn(char color) : Pieces(color) {}
    ~Pawn();
private:
    virtual char getPieceType() {
        return 'P';
    }
    bool areSquaresLegal(int srcRow, int srcCol, int destRow, int destCol, Pieces* board[8][8]);
    bool isLegalMove(int srcRow, int srcCol, int destRow, int destCol, Pieces* board[8][8]);
};

The last two methods are implemented in a .cpp file. Every other class is similar to the pawn one obviously.

When I try to compile, the builder gives me: undefined reference tovtable for Pawn'` with reference to the line where the constructor is:

Pawn(char color) : Pieces(color) {}

Where am I going wrong?

like image 300
Fed03 Avatar asked Sep 14 '12 11:09

Fed03


People also ask

Does abstract class have Vtable?

Yes, abstract classes do have vtables, also with pure abstract methods (these can actually be implemented and called), and yes - their constructor does initialize the pure entries to a specified value.

How do you fix a undefined reference in C++?

How To Fix Undefined Reference in C++ You can fix undefined reference in C++ by investigating the linker error messages and then providing the missing definition for the given symbols. Note that not all linker errors are undefined references, and the same programmer error does not cause all undefined reference errors.

How do you declare an abstract class in C++?

An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier ( = 0 ) in the declaration of a virtual member function in the class declaration.


1 Answers

You say you've implemented the last two member functions. My guess is that you haven't implemented the destructor that you've declared, but not defined in the class.

If you need a non-trivial destructor for the class, then make sure you implement it. If not, then remove its declaration.

In general, this error means that you've declared a non-pure virtual function and forgotten to implement it; some popular compilers place the class's polymorphic metadata in the same translation unit as the first non-pure, non-inline member function, which in this case is the destructor. If you see the error for a class that's supposed to be abstract, then it typically means that you've forgotten to declare some of its virtual functions pure.

like image 168
Mike Seymour Avatar answered Oct 23 '22 13:10

Mike Seymour