参考二维阵列

Sum*_*Tea 5 c++

我想用OpenGL实现一个函数来用C++渲染一个圆柱体.我的功能签名如下:

#define POINTS_NUM  15
#define DEMESION    3

void drawCylinder( int slices, int segments, GLfloat (&vertices)[ POINTS_NUM ][ DEMESION ] );
Run Code Online (Sandbox Code Playgroud)

我想使用对二维数组的引用来限制用户输入,但是发生了一些奇怪的行为.当我实现上面声明的函数时,会发生链接器错误:

错误1错误LNK2005:"float(*vase)[3]"(?vase @@ 3PAY02MA)已经在shapes.obj vase.obj中定义了VaseAndAnimation

这里vase定义为:

GLfloat vase[ POINTS_NUM ][ DEMESION ];
Run Code Online (Sandbox Code Playgroud)

起初,我认为最后一个维度有问题.所以我在第二次试验中省略了它.这次我的函数声明是这样的:

void drawCylinder( int slices, int segments, GLfloat (&vertices)[ POINTS_NUM ][] );
Run Code Online (Sandbox Code Playgroud)

现在调用时发生编译时错误(vase定义未更改):

drawCylinder( 10, 10, vase );
Run Code Online (Sandbox Code Playgroud)

编译错误:

错误1错误C2087:'vertices':缺少下标d:\ visual studio 2008\projects\project1\computer graphics\vaseandanimation\shapes.h 25 VaseAndAnimation

错误2错误C2664:'drawCylinder':无法将参数3从'GLfloat [14] [3]'转换为'GLfloat(&)[14] [1]'d:\ Visual Studio 2008\Projects\Project1\Computer Graphics\VaseAndAnimation\vase.cpp 64 VaseAndAnimation

错误3错误C2087:'vertices':缺少下标d:\ visual studio 2008\projects\project1\computer graphics\vaseandanimation\shapes.h 25 VaseAndAnimation

错误4错误C2087:'vertices':缺少下标d:\ Visual Studio 2008\Projects\Project1\Computer Graphics\VaseAndAnimation\shapes.cpp 12 VaseAndAnimation

从这个错误,我可以看到该参数vertices真的被视为对二维数组的引用,但为什么在我的第一个版本中vase解析float (* vase)[3]

我的IDE是Visual Studio 2008.我没有尝试过GCC; 这种行为依赖于编译器吗?

希望有人可以帮我摆脱陷阱.

Kir*_*sky 2

你的第一个声明没问题。看来你已经vase在头文件中定义了。