【问题标题】:c++ LNK2019 error between two classes in same project同一项目中两个类之间的c ++ LNK2019错误
【发布时间】:2016-08-25 22:36:30
【问题描述】:

所以我正在尝试创建一个模仿扫雷游戏的程序。我已经仔细检查了头文件、类名,并确保头文件 #included 在其他 cpp 文件中,但是当我尝试构建程序时,我在“主”类中收到 LNK2019 错误。

完全错误:

错误 1 ​​错误 LNK2019: 无法解析的外部符号 "public: __thiscall Board::Board(int,int,int)" (??0Board@@QAE@HHH@Z) 引用于 函数_main \fpsb\g\gathmr26\visual studio 2013\Projects\Minesweeper\Minesweeper\Main.obj 扫雷

我已经花了大约 2 个小时在 StackOverflow 和其他地方查看答案,但一无所获。我已经浏览了this MSDN page 中的每个要点,以及this popular answer 中的每个“共同原因”,但似乎没有一个适用于我的情况。我还尝试了 MSDN 页面上的所有“诊断工具”选项,他们所做的只是让我更加困惑。

我最接近我的情况(据我所知)是this question,除了我的所有代码都在一个项目中,而不是多个。回答这个问题的人之一说“我在我的 Visual Studio 中输入了这段代码,它运行良好”,假设文件在一个项目中。当我在这里遇到几乎相同的情况时,我不明白为什么该答案可以在那里工作。

所以,无论如何,这里是代码:

Main.cpp

#include <iostream>
#include <string>
#include "Cell.h"
#include "Board.h"

int main() {
    Board *bee;
    bee = new Board(50, 50, 50);

    std::cout << "board created";

    return 0;
}

Board.cpp

#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib> 
using namespace std;
#include "Cell.h"
#include "Board.h"
#ifndef BOARD_H
#define BOARD_H

// Board class. Used to create an array of cell objects to function as data model for Minsweeper game.
class Board
{
private:
int width; // number of columns in board
int height; // number of rows in board
int mines; // number of mines stored in board
Cell*** cells; // array storing cell objects

public:

// Constructor for board. Takes number of columns, rows, and mines as parameters
Board::Board(int cols, int rows, int numMines) {
    width = cols;
    height = rows;
    mines = numMines;
    cells = new Cell**[height];
    for (int i = 0; i < height; i++) {
        cells[i] = new Cell*[width];
    }
    int c = 0;
    int r = 0;
    while (r < height)
    {
        while (c < width)
        {
            setCell(c, r, CellValue::COVERED_CELL);
            c++;
        }
        c = 0;
        r++;
    }
    int m = 0;
    while (m < numMines)
    {
        std::srand(std::time(nullptr));
        int x = generateRandomNumberInRange(0, width - 1);
        int y = generateRandomNumberInRange(0, height - 1);
        if (!(getCellVal(x, y) == MINE))
        {
            setCell(x, y, CellValue::MINE);
            m++;
        }
    }
}

    // Accessor for width field
int Board::getWidth()
{
    return width;
}

// Accessor for height field
int Board::getHeight()
{
    return height;
}

// Accessor for mines field
int Board::getMines()
{
    return mines;
}

// Function to access value of cell located in array where x is column parameter and y is row parameter
CellValue Board::getCellVal(int x, int y)
{
    CellValue value = CellValue::INVALID_CELL;

    if (!(x < 0 || x >(width - 1) || y < 0 || y >(height - 1)))
    {
        Cell temp = *cells[x][y];
        value = temp.getValue();
    }

    return value;
}

// Function to set value of cell located in array where x is column parameter and y is row parameter
void Board::setCell(int x, int y, CellValue value)
{
    if (!(x < 0 || x >(width - 1) || y < 0 || y >(height - 1)))
    {
        Cell temp = *cells[x][y];
        temp.setValue(value);
    }
}

// Function to determine if game is lost
// Loops through array to see if there are any UNCOVERED_MINES
// If so, returns true, game ends, as you've lost :(
// If not, returns false and game can continue
// Should run after every click action in game
bool Board::isGameLost()
{
    bool isLost = false;
    int c = 0;
    int r = 0;
    while (r < height)
    {
        while (c < width)
        {
            if (getCellVal(c, r) == UNCOVERED_MINE)
            {
                isLost = true;
            }
            c++;
        }
        c = 0;
        r++;
    }
    return isLost;
}

// Function to determine if game is won
// Loops through array to determine if there are any falsely flagged mines, unflagged mines, covered cells, or uncovered mines
// If there are, returns false and game continues
// If not, returns true, games ends, you've won :)
bool Board::isGameWon()
{
    bool isWon = true;
    int c = 0;
    int r = 0;
    while (r < height)
    {
        while (c < width)
        {
            CellValue value = getCellVal(c, r);
            if ((value == FLAG) ||
                (value == MINE) ||
                (value == COVERED_CELL) ||
                (value == UNCOVERED_MINE))
            {
                isWon = false;
            }
            c++;
        }
        c = 0;
        r++;
    }
    return isWon;
}

};


#endif

Board.h

#include <iostream>
#include <string>
#include "Cell.h"
#ifndef BOARD_H
#define BOARD_H

class Cell;
enum CellValue;

class Board 
{
private: 
    int width;
    int height;
    int mines;
    Cell*** cells;

public:
    Board(int cols, int rows, int numMines);
    int getWidth();
    int getHeight();
    int getMines();
    CellValue* getCellVal(int x, int y);
    void setCell(int x, int y, CellValue value);
    void uncoverCell(int x, int y);
    void flagCell(int x, int y);
    bool isGameLost();
    bool isGameWon();
};

#endif

我知道这是人们常犯的错误,而且 StackOverflow 上有不少关于此的问题,但在这一点上,我还没有找到任何与我在这里遇到的问题相匹配的问题。这里有什么问题?

【问题讨论】:

    标签: c++ visual-c++ visual-studio-2013 lnk2019


    【解决方案1】:

    似乎您正在混合头文件和源文件。您的 cpp 文件包含一个 class 声明,其中定义了所有函数。这不是 cpp 文件的样子。它应该只包含函数声明:

    Board::Board(...)
    {
        ...
    }
    
    bool Board::IsGameWon...
    

    等等……

    【讨论】:

    • 大声笑我!显然,在 C++ 编程方面,我有很多东西要学。我现在构建了代码,现在可以修复运行时错误了!感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2018-08-27
    • 2018-06-07
    • 1970-01-01
    • 2013-02-03
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多