2016-04-30 25 views
1

私はMinesweeperゲームを模倣したプログラムを作成しようとしています。私はヘッダーファイルとクラス名を二重チェックし、ヘッダが他のcppファイルに#includeされていることを確認しましたが、プログラムをビルドしようとすると "Main"クラスにLNK2019エラーが発生します。フルでC++ LNK2019同じプロジェクトの2つのクラス間のエラー

エラー:

エラー1エラーLNK2019:未解決の外部シンボル "パブリック:__thiscall 会::会(int型、int型、int型)"(?? 0Board @@ QAE @ HHH @ Z ) 機能で参照_main \ fpsb \ G \ gathmr26 \ Visual Studioの 2013 \プロジェクト\マインスイーパ\マインスイーパ\ MAIN.OBJマインスイーパ

私は他の場所でのStackOverflowとに、ここの回答を見て、おそらく約2時間費やしてきましたどこにも行きませんでした。私はthis MSDN pageのすべての箇条書きと、すべての共通原因をthis popular answerに実行しましたが、私の状況にはあてはまりませんでした。私はMSDNページのすべての "診断ツール"オプションも試してみました。

私の状況に最も近いのはthis questionですが、私のコードはすべて1つのプロジェクトであり、複数ではありません。その質問に答えた人の一人は、「このコードを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

関連する問題