本文介绍了参考二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用OpenGL实现一个函数来渲染C ++中的圆柱。我的函数的签名如下:

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:

花瓶定义为:

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 ][] );

现在,当调用( vase 定义不变):

Now a compile-time error occurs when invoked as (vase definition isn't changed):

drawCylinder( 10, 10, vase );

编译错误:

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

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

Error 3 error C2087: 'vertices' : missing subscript d:\visual studio 2008\projects\project1\computer graphics\vaseandanimation\shapes.h 25 VaseAndAnimation

错误4错误C2087:'vertices':missing subscript d:\Visual Studio 2008 \Projects\Project1\Computer Graphics \VaseAndAnimation\shapes.cpp 12 VaseAndAnimation

Error 4 error C2087: 'vertices' : missing subscript d:\Visual Studio 2008\Projects\Project1\Computer Graphics\VaseAndAnimation\shapes.cpp 12 VaseAndAnimation

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

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?

我没有尝试过GCC;是行为编译器依赖?

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.

推荐答案

您的第一个声明很好。似乎是您已在头文件中定义 vase

Your first declaration is fine. Seems to be you have defined vase in a header file.

这篇关于参考二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 00:27