"错误LNK2019:未解决的外部致命问题"和"错误LNK1120:1未解析的外部"

时间:2016-11-28 18:12:39

标签: c++ visual-studio-2015 linker-errors

'解决' - 不要在标题和源文件中重复代码。

我是C ++的新手,我收到的错误似乎无法解决。

我目前正在使用Microsoft Visual Studios 2015(版本4.6.01586)并尝试编写我的第一个Chess主项目。

完整错误是:

  

Chess.obj:错误LNK2019:未解析的外部符号" public:__ thiscall Board :: Board(void)" (?? 0Board @@ QAE @ XZ)在函数_main中引用   C:\ Users \ Vince \ Google Drive \ Visual Studio \ Projects \ Chess \ Debug \ Chess.exe:致命错误LNK1120:1个未解析的外部

此链接器错误的常见解释似乎是用户选择了错误的项目类型(或忘记了主要功能);我已经检查了这个项目并且项目将会运行,前提是我没有尝试引用另一个类(即将打印" Hello World")。

我真的很感激任何帮助。

以下是.cpp文件的剪切和粘贴(它们也有相关的.h文件)和两个枚举.h文件

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 个答案:

答案 0 :(得分:-1)

您忘记在Board.cpp文件中使用命名空间std;&#34;&#34;