GLfloat array causes an error?

Hey,

I’m a beginner at OpenGL and not that great of a programmer. I’m compiling a pretty simple OpenGL/GLUT file under Windows using Visual Studio 2002 and I’m getting errors that I don’t know how to fix.

Here is the portion of the code that generates errors for me:

glEnable(GL_DEPTH_TEST);

// Enable a single OpenGL light.
// White diffuse light.
GLfloat light_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
// Put the light at infinity in the direction (1,1,1)
GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

The compiler outputs:
c:\My Documents\Visual Studio Projects\Cube\Cube\cube.c(167) : error C2275: ‘GLfloat’ : illegal use of this type as an expression
c:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(53) : see declaration of ‘GLfloat’
c:\My Documents\Visual Studio Projects\Cube\Cube\cube.c(167) : error C2065: ‘light_diffuse’ : undeclared identifier
c:\My Documents\Visual Studio Projects\Cube\Cube\cube.c(167) : error C2059: syntax error : ‘{’
c:\My Documents\Visual Studio Projects\Cube\Cube\cube.c(169) : error C2275: ‘GLfloat’ : illegal use of this type as an expression
c:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\gl\GL.h(53) : see declaration of ‘GLfloat’
c:\My Documents\Visual Studio Projects\Cube\Cube\cube.c(169) : error C2146: syntax error : missing ‘;’ before identifier ‘light_position’
c:\My Documents\Visual Studio Projects\Cube\Cube\cube.c(169) : error C2144: syntax error : ‘<Unknown>’ should be preceded by ‘<Unknown>’
c:\My Documents\Visual Studio Projects\Cube\Cube\cube.c(169) : error C2144: syntax error : ‘<Unknown>’ should be preceded by ‘<Unknown>’
c:\My Documents\Visual Studio Projects\Cube\Cube\cube.c(169) : error C2143: syntax error : missing ‘;’ before ‘identifier’
c:\My Documents\Visual Studio Projects\Cube\Cube\cube.c(169) : error C2065: ‘light_position’ : undeclared identifier
c:\My Documents\Visual Studio Projects\Cube\Cube\cube.c(169) : error C2059: syntax error : ‘]’
c:\My Documents\Visual Studio Projects\Cube\Cube\cube.c(170) : warning C4047: ‘function’ : ‘const GLfloat *’ differs in levels of indirection from ‘int’
c:\My Documents\Visual Studio Projects\Cube\Cube\cube.c(171) : warning C4047: ‘function’ : ‘const GLfloat *’ differs in levels of indirection from ‘int’

Every example file I’ve ever seen uses GLfloat in arrays. Any idea why my compiler seems to think this is illegal? Thanks!

Hi !

Have you included gl.h ?

Hi!

Thanks for the response. I did include gl.h

Thanks!

You’re doing several things that aren’t valid in C. Namely //-style comments and late temporary declarations. These things are only valid in C++.

Remove the source file from your project. Then rename it to “cube.cpp”. Readd it to the project. It should then compile fine.

Oh great you’re right. Thanks so much! It works now.