gpt4 book ai didi

c++ - 引用二维数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:41:58 24 4
gpt4 key购买 nike

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

#define POINTS_NUM  15
#define DEMESION 3

void drawCylinder( int slices, int segments, GLfloat (&vertices)[ POINTS_NUM ][ DEMESION ] );

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

Error 1 error LNK2005: "float (* vase)[3]" (?vase@@3PAY02MA) already defined in shapes.obj vase.obj VaseAndAnimation

此处花瓶定义为:

GLfloat vase[ POINTS_NUM ][ DEMESION ];

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

void drawCylinder( int slices, int segments, GLfloat (&vertices)[ POINTS_NUM ][] );

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

drawCylinder( 10, 10, vase );

编译错误:

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

从这个错误可以看出,参数vertices确实被当成了二维数组的引用,但是为什么vase被解析为float( * vase)[3] 在我的第一个版本中?

我的 IDE 是 Visual Studio 2008。我还没有用 GCC 试过;该行为是否依赖于编译器?

希望有人能帮我摆脱这个陷阱。

最佳答案

您的第一个声明没问题。似乎是您在头文件中定义了 vase

关于c++ - 引用二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3107246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com