1

我有一个奇怪的问题。我该如何解决它。

编译器报告中的错误指出:“16|undefined reference to `rpg::draw_grid(int, int)'”

我想知道您是否可以提供帮助。

谢谢

主文件

#include <iostream>
#include <cstdlib>
#include <ctime>
#include "rpg.h"

using namespace std;

int main()
{
    rpg class_rpg;

    int x = 4; //Co-ordinates
    int y = 4; //Co-ordinates
    char choice;

    class_rpg.draw_grid(x, y) ;

    while (x > -1 && x < 10 && y > -1 && y && 10){

        cout << "What direction shall you move?";
        cin >> choice;
        switch (choice){
            case 'u':
                x = x - 1;
                y = y;
                system("cls");
                class_rpg.draw_grid(x,y);
        }
    }


    cin.get();
    return 0;
}

rpg.h

#include <iostream>
#include <cstdlib>
#include <ctime>


#ifndef RPG_H
#define RPG_H


class rpg
{
    public:
        rpg();
        virtual ~rpg();
        void generate_world();
        void draw_grid(int x, int y);

        char grid[9][9] =      {{'.','.','.','.','.','.','.','.','.'},
                                {'.','.','.','.','.','.','.','.','.'},
                                {'.','.','.','.','.','.','.','.','.'},
                                {'.','.','.','.','.','.','.','.','.'},
                                {'.','.','.','.','.','.','.','.','.'},
                                {'.','.','.','.','.','.','.','.','.'},
                                {'.','.','.','.','.','.','.','.','.'},
                                {'.','.','.','.','.','.','.','.','.'},
                                {'.','.','.','.','.','.','.','.','.'}};

        char character = '*';
        char quest = 'Q';

        int position[2];
        int quest_position[2];

        int quest_coord[2];

        char direction;

        int x,y;
    protected:
    private:
};

#endif // RPG_H

rpg.cpp

#include <iostream>
#include <cstdlib>
#include <ctime>
#include "rpg.h"


using namespace std;

rpg::rpg()
{
    //ctor
}

rpg::~rpg()
{
    //dtor
}

void draw_grid(int x, int y)
{
    rpg class_rpg;

    int position[2] = {x,y};
    int quest_position[2];



    quest_position[0] = rand() % 9 + 0;
    quest_position[1] = rand() % 9 + 0;

    int quest_coord[2];

    quest_coord[0] = quest_position[0];
    quest_coord[1] = quest_position[1];




    for(int i = 0; i < 9; i++){
        for (int j = 0; j < 9; j++){
            if(i == position[0] && j == position[1])
                cout << class_rpg.character;
            else if(i == quest_coord[0] && j == quest_coord[1])
                cout << class_rpg.quest;
            else
                cout << class_rpg.grid[i][j];
            cout << " ";
        }
        cout << endl;
       }
}
4

1 回答 1

1

根据编译器错误信息:

未定义对 `rpg::draw_grid(int, int) 的引用

这意味着编译器无法draw_gridrpg.

您应该告诉编译器它draw_grid是 class 的成员rpg

 void rpg::draw_grid(int x, int y)
 {     //^^You missed this part
于 2013-06-01T19:15:31.787 回答