Pieter
Pieter

Reputation: 32755

Undefined reference to enum

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

Answers (3)

David Brown
David Brown

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

kennytm
kennytm

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

David Norman
David Norman

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

Related Questions