未定义引用'rpg :: draw grid(int,int)'

时间:2013-06-01 19:13:47

标签: c++

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

编译器报告中的错误指出:“16 |未定义引用`rpg :: draw_grid(int,int)'”

我想知道你是否可以提供协助。

由于

的main.cpp

#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;
       }
}

1 个答案:

答案 0 :(得分:1)

根据编译器错误消息:

  

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

这意味着编译器无法在draw_grid中找到成员rpg的定义。

您应该告诉编译器draw_grid是类rpg的成员:

 void rpg::draw_grid(int x, int y)
 {     //^^You missed this part