'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
};