C++ 错误:';纹理ID';是';纹理';
“textureID是Texture的私有成员”错误的解决方案是什么。Texture是一个类,textureID是一个无符号整数。程序运行的图像是一个24位未压缩位图(我想)。错误出现在glBindTexture(GL_TEXTURE_2D,tex->textureID)处。下面是代码的一部分C++ 错误:';纹理ID';是';纹理';,c++,opengl,textures,glut,C++,Opengl,Textures,Glut,“textureID是Texture的私有成员”错误的解决方案是什么。Texture是一个类,textureID是一个无符号整数。程序运行的图像是一个24位未压缩位图(我想)。错误出现在glBindTexture(GL_TEXTURE_2D,tex->textureID)处。下面是代码的一部分 void display() { preProcessEvents(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
void display() {
preProcessEvents();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//Camera Transformations
glRotatef(Camera::rotation.x, 1, 0, 0);
glRotatef(Camera::rotation.y, 0, 1, 0);
glRotatef(Camera::rotation.z, 0, 0, 1);
glTranslatef(-Camera::position.x, -Camera::position.y, -Camera::position.z);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-1, 0, -3);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0, 2, -3);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(1, 0, -3);
glEnd();
glBindTexture(GL_TEXTURE_2D, tex->textureID);
glBegin(GL_QUADS);
glColor3f(1, 1, 1);
glTexCoord2f(100, 100);
glVertex3f(100, 0, 100);
glTexCoord2f(-100, 100);
glVertex3f(-100, 0, 100);
glTexCoord2f(-100, -100);
glVertex3f(-100, 0, -100);
glTexCoord2f(100, -100);
glVertex3f(100, 0, -100);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glutSwapBuffers();
}
该类位于头文件中,代码如下。“unsigned int textureID”应该是公共的还是其他的?这就是为什么错误会说textureID是私有的吗
#ifndef __Xcode_Glut_Tutorial__Texture__
#define __Xcode_Glut_Tutorial__Texture__
#include <iostream>
#include <OpenGL/gl.h>
using namespace std;
class Texture {
unsigned int textureID;
public:
Texture(void* data, int w, int h, int format);
static Texture* loadBMP(const char* filename);
};
#endif /* defined(__Xcode_Glut_Tutorial__Texture__) */
\ifndef\uuuxcode\uglut\u教程\uuuu纹理__
#定义\uuuxcode\uglut\u教程\uuuu纹理__
#包括
#包括
使用名称空间std;
类纹理{
无符号整数;
公众:
纹理(void*数据、int w、int h、int格式);
静态纹理*加载BMP(常量字符*文件名);
};
#endif/*已定义(\uuuxcode\uglut\u教程\uuuuuuu纹理\uuuuuu)*/
textureID
在Texture
类中是私有的
解决方案1:
您必须更改:
class Texture {
unsigned int textureID;
public:
Texture(void* data, int w, int h, int format);
static Texture* loadBMP(const char* filename);
};
class Texture {
unsigned int textureID;
public:
Texture(void* data, int w, int h, int format);
static Texture* loadBMP(const char* filename);
unsigned int get_textureID()
{
return textureID;
}
};
到
解决方案2:
使用公共getter函数:
class Texture {
unsigned int textureID;
public:
Texture(void* data, int w, int h, int format);
static Texture* loadBMP(const char* filename);
};
class Texture {
unsigned int textureID;
public:
Texture(void* data, int w, int h, int format);
static Texture* loadBMP(const char* filename);
unsigned int get_textureID()
{
return textureID;
}
};
和:
glBindTexture(GL_TEXTURE_2D, tex->get_textureID());
它是私有的,所以你不能像那样访问它,要么公开它,要么添加一个getter函数如果你只是对值感兴趣默认情况下,类中的访问规范是私有的。因为您没有将变量公开,所以它不是。我尝试将“unsigned int textureID”公开,方法是在public下声明它。我得到一个“构建成功”。然而,在屏幕的左侧,我看到几个线程在程序中有断点。也许我以不正确的方式声明了“unsigned int textureID”?尝试了两种解决方案。这两个版本的“构建成功”。在左手边收到一束线。一定还有别的原因。我会一直考虑的。