c++ - 在我的 C++ 游戏中出现 "undefined reference to class::function"错误。

标签 c++ class reference undefined member-functions

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

#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++ 游戏中出现 "undefined reference to class::function"错误。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23860616/

相关文章:

c++ - 重载 << 运算符

Java:如何从泛型类型中获取类文字?

java - 如何使用正在读取的文本文件的值填充 "ragged"数组?

java - 在java中重新分配其复制引用时更改原始对象

c++ - boost::scoped_ptr 的 NULL 检查

c++ - 如何从线程中取回数据?

Perl Hash 引用

c++ - 在 C++ 中引用 stack<int>.top() 的返回值的正确行为是什么

c++ - 使用函数指针专门化模板,这取决于模板参数

javascript - 为什么这里需要 static 关键字