【问题标题】:Error: 'textureID' is a private member of 'Texture'错误:“textureID”是“Texture”的私有成员
【发布时间】:2014-09-15 09:50:53
【问题描述】:

错误“textureID is a private member of Texture”的解决方法是什么。 Texture 是一个类,而 textureID 是一个无符号整数。程序运行的图像是 24 位未压缩位图(我认为)。错误出现在 glBindTexture(GL_TEXTURE_2D, tex->textureID)。以下是部分代码。

    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__) */

【问题讨论】:

  • 好吧,它是私有的,所以你不能像那样访问它,如果你只对值感兴趣,要么将其公开,要么添加一个 getter 函数
  • 默认情况下,类中的访问规范是私有的。由于您尚未公开该变量,因此未公开。
  • 我尝试通过将 'unsigned int textureID' 声明为 public 来公开它。我得到一个“构建成功”。但是,在屏幕的左侧,我得到了几个在程序中有断点的线程。也许我以不正确的方式声明了“unsigned int textureID”?

标签: c++ opengl textures glut


【解决方案1】:

textureIDTexture 类中是私有的

解决方案 1:
你必须改变:

class Texture {
    unsigned int textureID;
public:
    Texture(void* data, int w, int h, int format);

    static Texture* loadBMP(const char* filename);
};

class Texture {
public:
    unsigned int textureID;

    Texture(void* data, int w, int h, int format);

    static Texture* loadBMP(const char* filename);
};


解决方案 2:
使用公共 getter 函数:

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());

【讨论】:

  • 尝试了这两种解决方案。两者都“构建成功”。在左侧收到一堆线程。这一定有别的原因。我会继续考虑的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-30
  • 1970-01-01
  • 1970-01-01
  • 2015-05-17
  • 1970-01-01
  • 2017-09-04
  • 1970-01-01
相关资源
最近更新 更多