I want to implement a function with OpenGL to render a cylinder in C++. The signature of my function is as follows:
#define POINTS_NUM 15
#define DEMESION 3
void drawCylinder( int slices, int segments, GLfloat (&vertices)[ POINTS_NUM ][ DEMESION ] );
I want to use a reference to a two-dimensional array to limit user input, but some strange behavior is happening. When I implement the function declared as above, an linker error occurs:
Error 1 error LNK2005: "float (* vase)[3]" (?vase@@3PAY02MA) already defined in shapes.obj vase.obj VaseAndAnimation
Here vase
is defined as:
GLfloat vase[ POINTS_NUM ][ DEMESION ];
At first, I thought there was something wrong with the last dimension. So I omitted it in my second trial. This time declaration of my function is like this:
void drawCylinder( int slices, int segments, GLfloat (&vertices)[ POINTS_NUM ][] );
Now a compile-time error occurs when invoked as (vase
definition isn't changed):
drawCylinder( 10, 10, vase );
Compile error:
Error 1 error C2087: 'vertices' : missing subscript d:\visual studio 2008\projects\project1\computer graphics\vaseandanimation\shapes.h 25 VaseAndAnimation
Error 2 error C2664: 'drawCylinder' : cannot convert parameter 3 from 'GLfloat [14][3]' to 'GLfloat (&)[14][1]' d:\Visual Studio 2008\Projects\Project1\Computer Graphics\VaseAndAnimation\vase.cpp 64 VaseAndAnimation
Error 3 error C2087: 'vertices' : missing subscript d:\visual studio 2008\projects\project1\computer graphics\vaseandanimation\shapes.h 25 VaseAndAnimation
Error 4 error C2087: 'vertices' : missing subscript d:\Visual Studio 2008\Projects\Project1\Computer Graphics\VaseAndAnimation\shapes.cpp 12 VaseAndAnimation
From this error, I can see that parameter vertices
is really treated a reference to a two-dimensional array, but why is vase
parsed as float (* vase)[3]
in my first version?
My IDE is Visual Studio 2008. I haven't tried it with GCC; is that behavior compiler-dependent?
Hope someone can give me a hand to get rid of the trap.