Undefined listing link

I get this error message from my compiler:

undefined reference to `Pawn::Pawn(Piece::Color)'

This happens when I do this:

// board[][] contains pointers to Piece objects
board[0][0] = new Pawn(Piece::BLACK);

Here is 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 is part of the Piece class:

class Piece {
public:
        // ...

        enum Color {WHITE, BLACK};

        // ...
};

Why am I getting this compiler error?

+3
source share
3 answers

The error has nothing to do with enumeration. You need to define a Pawn (Color) constructor, for example,

Pawn::Pawn(Color)
{
...
}
+7
source

You need to define a function body for the constructor.

This code gives a linker error: http://www.ideone.com/pGOkn

    Pawn(Color color) ;

This code will not be: http://www.ideone.com/EkgMS

    Pawn(Color color) {}
    //                ^^ define the constructor to do nothing.
+5
source

, , Pawn::Pawn(Color). Pawn::Pawn(Color) ?

+4
source

Source: https://habr.com/ru/post/1770789/


All Articles