我有几节课。
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:错误:未在此范围内声明“英雄”
它出了什么问题?
已添加:主要内容如下:
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();
答案 0 :(得分:1)
使用类似的参数声明你的函数
void DRAW :: draw_hero(HERO * hero) {/* rest of the code*/}
并在调用时将指向创建对象hero
的指针传递给函数。