2016-04-30 19 views
1

所以我試圖創建一個模仿掃雷遊戲的程序。我已經仔細檢查了頭文件和類名,並且確保頭文件包含在其他cpp文件中,但是當我嘗試構建程序時,我在「Main」類中得到了一個LNK2019錯誤。C++ LNK2019在同一個項目中的兩個類之間的錯誤

錯誤全:

錯誤1個錯誤LNK2019:無法解析的外部符號 「公用:__thiscall 局::局(INT,INT,INT)」(?? 0Board @@ QAE @ HHH @ Z )在 函數引用_main \ FPSB一個\ g \ gathmr26 \ Visual Studio的 2013 \項目\掃雷\掃雷\ Main.obj掃雷

我花了大概2小時左右,在尋找答案,這裏在計算器上和其他地方並沒有得到任何好處。我已經在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上有很多這方面的問題,但在這一點上,我還沒有發現任何與我在這裏相匹配的東西。這裏有什麼問題?

回答

3

好像你在混合標題和源文件。您的cpp文件包含一個class聲明,其中包含所有定義的函數。這不是一個cpp文件的樣子。它應該只包含函數聲明:

Board::Board(...) 
{ 
    ... 
} 

bool Board::IsGameWon... 

等等

+0

大聲笑我傻!顯然,當談到C++編程時,我有很多東西需要學習。我現在得到了代碼構建,現在它已關閉以解決運行時錯誤!感謝您的幫助 – JaykeBird

相關問題