1

我有几节课。

class DRAW {
private:
    unsigned int back_texture;
    unsigned int hero_texture;

public:
    DRAW();
    ~DRAW();
    void start_draw();
    void draw_back();
    void draw_hero();
    void end_draw();

};
class HERO {

private:
    float x,y,w,h; //coordinates
    float hp; //health

public:
    friend class DRAW;

    HERO(float x1, float y1, float w1, float h1, float hp1);
    ~HERO();

    void set_posX(float x1);
    void set_posY(float y1);
    void set_height(float h1);
    void set_width(float w1);
    void set_health(float hp1);

    float get_posX();
    float get_posY();
    float get_height();
    float get_width();
    float get_health();

    void move(char direrion);

};

我在程序的主函数(hero = new HERO())中创建了对象。我正在尝试从 draw_hero() 函数访问对象 hero 的参数。

void DRAW :: draw_hero() {

glBindTexture(GL_TEXTURE_2D, hero_texture);

glBegin(GL_QUADS);

glTexCoord2d(0,0); glVertex2f(hero->get_posX(),hero->get_posY());
glTexCoord2d(1,0); glVertex2f(hero->get_posX() + hero->get_width(),hero->get_posY());
glTexCoord2d(1,1); glVertex2f(hero->get_posX() + hero->get_width(),hero->get_posY() + hero -> get_height());
glTexCoord2d(0,1); glVertex2f(hero->get_posX(),hero->get_posY());
glEnd();

}

当我编译时,我发现: draw.h:83:32: error: 'hero' is not declared in this scope

它有什么问题?

添加:主要看起来像这样:

int main (int argc, char* argv[] ) {

 /* DECLARATION */

SDL_Event event;

SYSTEM* system;

bool isRunning = true;

Uint8* key;

static HERO* hero;
hero = new HERO(300,300, 40, 40, 100);

DRAW* image = new DRAW;
DRAW();
.
.
.
    /* RENDERING */


    image -> start_draw();
    image -> draw_back();
    image -> end_draw();
4

1 回答 1

1

用类似的参数声明你的函数

void DRAW :: draw_hero(HERO * hero) {/* rest of the code*/}

并在调用时将指向已创建对象的指针传递hero给函数。

于 2012-04-11T23:52:40.080 回答