views:

41

answers:

3

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?

+6  A: 

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)
{
...
}
David Norman
Wow... I can't believe I completely forgot to write the constructor. I think I'll need to try that coffee thing I've heard so much about.
Pieter
+5  A: 

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.
KennyTM
+4  A: 

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?

Nali4Freedom