无法从其他类的功能访问对象的参数
Can't access object's parameters from the other class's func
我有几个类。
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
传递给函数。
相关文章:
- 如何从其他功能C++访问参数?
- 如何使用 MPI 的远程内存访问 (RMA) 功能并行化数据聚合?
- 访问我以前可以访问的功能时出现分段错误
- 好友功能 - 成员无法访问
- 如何启用友元类的友元功能直接在C++中访问其私有成员
- 无法从好友功能访问类的私有成员?"ostream"不是"std"的成员?
- 无法访问公共功能? 没有指定成员?
- 好友功能仍无法访问私人会员
- 我无法获得好友会员功能以实际访问私人会员
- GPU设备函数如何访问主机功能中定义的对象
- 派生类中的成员字段别名(无访问器功能)
- 从C文件访问C 类成员功能
- 访问嵌套结构的数据成员并将结构传递到功能C
- 尝试在 qt 中建立访问器功能
- (C )创建可以从功能访问的动态全局数组/向量
- 无法从类中的其他功能访问同一类的变量
- 防止朋友功能访问班级的私人会员
- 使用类成员功能访问私有结构
- 成员无法使用好友功能访问
- 无法从其他类的功能访问对象的参数