Weird Error: shader is invalid

hello,
class ShaderProgram {
public:
ShaderProgram(GLuint id) {
m_uiProgramID = id;
}
GLuint programID() {
return m_uiProgramID;
}
}; // this class is defined in another file
when i use it like this:
GLuint tmpID = create and link program; // this is correct.
ShaderProgram p(tmpID);
GLuint id = p.programID(); // this does not correct.

but it is ok like this:
ShaderProgram *p = new ShaderProgram(tmpID);
GLuint id = p->programID();

why? The program id is not just a int?? what if it is copied? the program object will lost?

Is this is the problem
ShaderProgram(GLuint id) {
m_uiProgramID = id;
}

// m_uiProgramID = id; id is not copied to member variable m_uiProgramID

No more problem found in this code.

I faced another problem when creating static object of my shader class.

Its reason was, the rendering context is not created and not called wglMakeCurrent before calling wglGetProcAddress.

If rendering context is not active for the current thread, then wglGetProcAddress returns NULL.

thank you, i make a mistake in the code, but that is not the cause.

Either should be just fine. This is not an OpenGL problem you’re having but a basic C++ problem apparently.

Post a small “complete” test program that illustrates your problem. For instance, this test prog disproves your assertion:


#include <GL/gl.h>
#include <stdio.h>

class ShaderProgram {
  GLuint m_uiProgramID;
public:
  ShaderProgram(GLuint id) {
    m_uiProgramID = id;
  }

  GLuint programID() {
    return m_uiProgramID;
  }
};

int main()
{
  GLuint tmpID = 123;
  ShaderProgram p(tmpID);
  GLuint id = p.programID();

  printf( "This better be 123: %u
", id );
  return 0;
}

The program id is not just a int??

No, the program handle is exactly a GLuint (unsigned int).

what if it is copied? the program object will lost?

No, this is C/C++, and this is just an unsigned int handle. As you know, there is no built-in garbage collection in C/C++. Nothing mysterious going on here.

Post a short “complete” test program that illustrates your problem.