我正在为大学工作编写此游戏,我相信自己的代码是正确的,但是我一直遇到此错误,这使我无法及时完成工作。所以这是游戏板类标题:

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstdio>

class CMagicAlchemistBoard
{
public:

CMagicAlchemistBoard(void); //  Default Constructor
CMagicAlchemistBoard(const CMagicAlchemistBoard& board); //  Copy Constructor
~CMagicAlchemistBoard(void ); //  Destructor
void SetupBoard(void);  // Function to setup the board
int GetBoardSpace(int row, int col); // Get the color at row,col

//  Accessor functions to get/set board size information
int GetColumns(void) const { return m_nColumns; }
void SetColumns(int nColumns)  { m_nColumns = (nColumns >= 6) ? nColumns : 6; }
int GetRows(void) const { return m_nRows; }
void SetRows(int nRows)  { m_nRows = (nRows >= 8) ? nRows : 8; }

void DeleteBoard(void); //  Function to delete the board and free memory
void ExecuteMove(int row, int col);
bool IsGameOver(void) const;  //  Is the game over?
void DrawBoard(void);
bool ValidMove(int row, int col); // Function to see if a move is valid

private:

void CreateBoard(void); //Function to create the board and allocate memory
// Class Data
int** m_arrBoard; // 2D array pointer
//  Board size information
char m_arrChars[10];
int m_nColumns;
int m_nRows;
};


这是仅带有DrawBoard()函数实现的.cpp文件:

#include "cmagicalchemistboard.h"
using namespace std;
void CMagicAlchemistBoard::DrawBoard(void)
{
cout << "MAGIC ALCHEMIST" << endl;
cout << "  ";
for(int col = 0; col < m_nColumns; col++){ printf(" ---",col); }
cout << endl;
for(int row = 0; row < m_nRows; row++)
{
    for(int col = 0; col < m_nColumns; col++)
    {
        cout << "|  " << m_arrChars[GetBoardSpace(row, col)];
    }
    cout << "| " << endl;
}
}


我假装在另一个类上使用此功能。这是该类的标题:

#include "cmagicalchemistboard.h"
#include <iostream>
#include <cstdlib>
#include <conio.h> //Contains the function getch(), which reads the input from the     keyboard
#define LEFT_ARROW  75
#define RIGHT_ARROW  77
#define UP_ARROW 72
#define DOWN_ARROW 80
#define ESC 27

class CMagicAlchemist
{
public:
    CMagicAlchemist();
    ~CMagicAlchemist();
     // Functions for accessing the game board
    char GetBoardSpace(int row, int col) { return m_board->GetBoardSpace(row,     col); }
    void SetupBoard(void)         { m_board->SetupBoard(); }
    int GetColumns(void)          { return m_board->GetColumns(); }
    void SetColumns(int nColumns) { m_board->SetColumns(nColumns); }
    int GetRows(void)             { return m_board->GetRows(); }
    void SetRows(int nRows)       { m_board->SetRows(nRows); }
    void DeleteBoard(void)        { m_board->DeleteBoard(); }
    bool IsGameOver()             { return m_board->IsGameOver(); }
    void InputGameParameters();
    void GetMove(int &row, int &col);
    void DrawBoard();
    void NewGame();
    void Game();


private:
    CMagicAlchemistBoard* m_board; // Instance of the game board
    int m_nmoves;
};


最后,最后一个类的.cpp文件仅包含调用DrawBoard()函数的函数的实现:

#include "cmagicalchemist.h"
void CMagicAlchemist::Game()
{
int x,y;
InputGameParameters();
NewGame();
DrawBoard();
}


因此,我的问题是:当我编译该程序时,我收到此错误:“对CMagicAlchemist :: DrawBoard()的未定义引用”。这是愚蠢的,因为DrawBoard()函数甚至不属于CMagicAlchemist类,而是属于CMagicAlchemistBoard类。有人可以帮我吗?

最佳答案

您已经在CMagicAlchemist类中声明了DrawBoard。
您可以从CMagicAlchemist成员函数调用DrawBoard。

试图为CMagicAlchemist类调用Drawboard。

关于c++ - 在我的C++游戏中收到“对class::function的 undefined reference ”错误。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23860616/

10-12 01:34