无法从其他类的功能访问对象的参数

Can't access object's parameters from the other class's func

本文关键字:功能 访问 对象 参数 其他      更新时间:2023-10-16

我有几个类。

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() 函数访问对象英雄的参数。

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:错误:"hero"未在此范围内声明

它有什么问题?

补充:主要看起来像这样:

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();
用这样的

参数声明你的函数

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

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