Reputation: 32755
I'm getting this error message from my compiler:
undefined reference to `Pawn::Pawn(Piece::Color)'
This occurs when I do this:
// board[][] contains pointers to Piece objects
board[0][0] = new Pawn(Piece::BLACK);
Here's part of the Pawn class:
// Includes...
#include "piece.h"
// Includes...
class Pawn : public Piece {
public:
// ...
// Creates a black or white pawn.
Pawn(Color color);
// ...
};
Here's part of the Piece class:
class Piece {
public:
// ...
enum Color {WHITE, BLACK};
// ...
};
Why am I getting this compiler error?
Upvotes: 3
Views: 2322
Reputation: 13526
The problem isn't with the enum, it's that the linker can't find the implementation of Pawn::Pawn(Color)
. Have you implemented the Pawn::Pawn(Color)
constructor and is it being linked here?
Upvotes: 4
Reputation: 523264
You need to define a function body for the constructor.
This code gives the linker error: http://www.ideone.com/pGOkn
Pawn(Color color) ;
This code will not: http://www.ideone.com/EkgMS
Pawn(Color color) {}
// ^^ define the constructor to do nothing.
Upvotes: 5
Reputation: 19879
The error doesn't really have anything to do with the enum. You need to define the Pawn(Color) constructor, e.g.,
Pawn::Pawn(Color)
{
...
}
Upvotes: 7