2016-04-30 6 views
1

Also versuche ich ein Programm zu erstellen, das das Minesweeper-Spiel nachahmt. Ich habe die Header-Dateien, die Klassennamen, überprüft und sichergestellt, dass die Header in den anderen cpp-Dateien enthalten sind, aber wenn ich versuche, das Programm zu erstellen, bekomme ich einen LNK2019-Fehler in der "Main" -Klasse, die ich habe.C++ LNK2019 Fehler zwischen zwei Klassen in einem Projekt

Fehler in voller Länge:

Fehler 1 Fehler LNK2019: nicht aufgelöstes externes Symbol "public: __thiscall Forum :: Board (int, int, int)" (?? 0Board @@ QAE @ HHH @ Z) in Funktion verwiesen _main \ FPSB \ g \ gathmr26 \ Visual Studio 2013 \ Projects \ Sweeper \ Sweeper \ Main.obj Sweeper

ich habe wahrscheinlich etwa 2 Stunden verbrachte hier auf Stackoverflow bei Antworten zu suchen und anderswo und kam nirgendwohin. Ich bin durch alle Aufzählungszeichen in this MSDN page gelaufen, und jede "gemeinsame Ursache" in this popular answer, und keiner von ihnen schien auf meine Situation zutreffen. Ich habe auch alle "Diagnose-Tools" -Optionen auf der MSDN-Seite ausprobiert und alles, was sie getan haben, verwirrt mich nur noch mehr.

Die nächste, die ich zu meiner Situation habe (soweit ich das beurteilen kann) ist this question mit der Ausnahme, dass alle meine Code nur in einem Projekt, nicht mehrere ist. Einer der Leute, die diese Frage beantwortet haben, sagte: "Ich habe diesen Code in mein Visual Studio eingegeben und es hat gut funktioniert", nachdem ich angenommen hatte, dass die Dateien in einem Projekt waren. Ich verstehe nicht, warum diese Antwort funktioniert hat, wenn ich hier die gleiche Situation habe.

Also, wie auch immer, hier ist der Code:

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

Ich weiß, dass dies ein häufiger Fehler ist, den die Leute haben und dass es auf StackOverflow mehr als eine Handvoll Fragen dazu gibt, aber zu diesem Zeitpunkt habe ich keine gefunden, die dem hier entsprechen. Was ist das Problem hier?

Antwort

3

Scheint so, als würden Sie Header- und Quelldateien mischen. Ihre cpp-Datei enthält eine class Deklaration mit allen darin definierten Funktionen. So sieht eine cpp-Datei nicht aus. Es sollte nur Funktionsdeklarationen enthalten:

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

bool Board::IsGameWon... 

etc ...

+0

Lol dumm von mir! Ich habe viel zu lernen, wenn es um C++ - Programmierung geht. Ich habe jetzt den Codeaufbau und jetzt ist es dran, Laufzeitfehler zu beheben! Danke für die Hilfe – JaykeBird

Verwandte Themen