0

'Solved' - don't repeat code in header and source files.

I'm new to C++ and I am getting an error that I cannot seem to solve.

I am currently using Microsoft Visual Studios 2015 (ver 4.6.01586) and trying to code my first main project of Chess.

The full error is:

Chess.obj : error LNK2019: unresolved external symbol "public: __thiscall Board::Board(void)" (??0Board@@QAE@XZ) referenced in function _main C:\Users\Vince\Google Drive\Visual Studio\Projects\Chess\Debug\Chess.exe : fatal error LNK1120: 1 unresolved externals

The common explanation for this Linker error seems to be that the user has selected the wrong project type (or forgotten to have a main function); I've checked this and the project will run provided that I don't try to reference another class (ie will print "Hello World").

I would really appreciate any help.

Here are cut and pastes of the .cpp files (they have associated .h files too) and the two enum .h files

Chess.cpp

// Chess.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "board.h"
#include "colour.h"
using namespace std;


int main(int argv, char* argc[])    {

    cout << "Hello World";

    Board* game = new Board();

    return 0;
}

Board.cpp

#include "stdafx.h"
#include "colour.h"
#include "player.h"

class Board {
private:
    //Type board[8][8];
    Player* players[2];

public: 
    Board() {
        players[0] = new Player(Colour::black);
        players[1] = new Player(Colour::white);
    }

    void draw_board() {

    }
};

Player.cpp

#include "stdafx.h"
#include "colour.h"
#include "piece.h"
using namespace std;

class Player {
private:
    Colour colour;

    Pawn* pawns[8];
    Rook* rooks[2];
    Knight* knights[2];
    Bishop* bishops[2];
    Queen* queen;
    King* king;
public:
    Player(Colour cl) {
        colour = cl;

        int row1, row2, queen_pos, king_pos; // row1 is front row, row2 is back row. 
        if (colour == black) { // black is top
            row1 = 1;
            row2 = 0;
            queen_pos = 3;
            king_pos = 4;
        }
        else    {  // white is bottom
            row1 = 6;
            row2 = 7;
            queen_pos = 4;
            king_pos = 3;
        }

        for (int i = 0; i < 8; i++) {
            pawns[i] = new Pawn(i, row1, colour);
        }

        rooks[1] = new Rook(0, row2, colour);
        rooks[2] = new Rook(7, row2, colour);
        knights[1] = new Knight(1, row2, colour);
        knights[2] = new Knight(6, row2, colour);
        bishops[1] = new Bishop(2, row2, colour);
        bishops[2] = new Bishop(5, row2, colour);
        queen = new Queen(queen_pos, row2, colour);
        king = new King(king_pos, row2, colour);
    }
};

Piece.cpp

#include "stdafx.h"
#include "type.h"
#include "colour.h"
using namespace std;


class Piece {
protected:
    int coords[2];
    Colour colour;
    bool captured = false;

public:

};

class Pawn : Piece {
public:
    Pawn(int x, int y, Colour cl) {
        coords[1] = x;
        coords[2] = y;
        colour = cl;
    }

    Type getType() {
        return PAWN;
    }

    string str_piece()    {
        char cl = 'W';
        string str;
        if (colour == black) cl = 'B';
        str = cl + "-P";
        return str;
    }
};

class Rook : Piece {
public:
    Rook(int x, int y, Colour cl) {
        coords[1] = x;
        coords[2] = y;
        colour = cl;
    }

    Type getType() {
        return ROOK;
    }

    string str_piece()    {
        char cl = 'W';
        string str;
        if (colour == black) cl = 'B';
        str = cl + "-R";
        return str;
    }
};

class Knight : Piece {
public:
    Knight(int x, int y, Colour cl) {
        coords[1] = x;
        coords[2] = y;
        colour = cl;
    }

    Type getType() {
        return KNIGHT;
    }

    string str_piece()    {
        char cl = 'W';
        string str;
        if (colour == black) cl = 'B';
        str = cl + "Kn";
        return str;
    }
};

class Bishop : Piece {
public:
    Bishop(int x, int y, Colour cl) {
        coords[1] = x;
        coords[2] = y;
        colour = cl;
    }

    Type getType() {
        return BISHOP;
    }

    string str_piece()    {
        char cl = 'W';
        string str;
        if (colour == black) cl = 'B';
        str = cl + "-B";
        return str;
    }
};

class Queen : Piece {
public:
    Queen(int x, int y, Colour cl) {
        coords[1] = x;
        coords[2] = y;
        colour = cl;
    }

    Type getType() {
        return QUEEN;
    }

    string str_piece()    {
        char cl = 'W';
        string str;
        if (colour == black) cl = 'B';
        str = cl + "-Q";
        return str;
    }
};

class King : Piece {
public:
    King(int x, int y, Colour cl) {
        coords[1] = x;
        coords[2] = y;
        colour = cl;
    }

    Type getType() {
        return KING;
    }

    string str_piece()    {
        char cl = 'W';
        string str;
        if (colour == black) cl = 'B';
        str = cl + "-K";
        return str;
    }
};

colour.h

#pragma once

#include "stdafx.h"

enum Colour {
    white, black
};

type.h

#pragma once

#include "stdafx.h"

enum Type {
    EMPTY, PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING
};
  • 1
    What is in `board.h` if you declare your `Board` class in `board.cpp`? – drescherjm Nov 28 '16 at 18:14
  • What does the header for `Board.cpp` look like? It appears you've defined the entire class in the .cpp file. – AndyG Nov 28 '16 at 18:15
  • ***(please let me know if this doesn't work)*** Will this code be available 20 years from now so that the question still makes sense? For StackOverflow a minimal example must exist in the question. You must not rely on an external link. Remember the point of StackOverflow is to help future readers with common problems. – drescherjm Nov 28 '16 at 18:17
  • #pragma once #include "stdafx.h" #include "colour.h" #include "player.h" class Board { private: Player players[2]; public: Board(); void draw_board(); }; not easy to read. are you not supposed to declare everything in the .cpp?? – Vince Martin Nov 28 '16 at 18:17
  • ***you not supposed to declare everything in the .cpp*** No that goes in the header. You don't duplicate the contents of the header in the source file. – drescherjm Nov 28 '16 at 18:19
  • Thanks I'll have a look at that – Vince Martin Nov 28 '16 at 18:21
  • Thank you for the help. I've moved all the code from the .cpp files to the .h files (and deleted the .cpp files) and it now works. – Vince Martin Nov 28 '16 at 18:31
  • It's a fair point about the link, I have now removed it. – Vince Martin Nov 28 '16 at 18:35

1 Answers1

-1

You forgot to put "using namespace std;"in your Board.cpp file.

  • 1
    That's a bad idea, a lot of the time. [Why is “using namespace std” considered bad practice?](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – user4581301 Nov 28 '16 at 19:18