简体   繁体   English

如何通过数组传递多参数函数? -C ++

[英]How Can I Pass a Multi Argument Function with an Array? - C++

I am trying to pass an array containing an X, Y, and Z coordinate (type float) to the function glTranslatef() however I can't figure out a way to accomplish this. 我试图将包含X,Y和Z坐标(类型为float)的数组传递给函数glTranslatef(),但是我不知道实现此目的的方法。

As an example of what I am trying to accomplish; 作为我要完成的事情的一个例子;

float xyz[3] = {3,2,1};
glTranslatef(xyz);

I get the following error whenever I try to attempt something like this 每当我尝试这样的事情时,我都会收到以下错误

cannot convert 'float*' to 'GLfloat {aka float}' for argument '1' to 'void glTranslatef(GLfloat, GLfloat, GLfloat)' 无法将参数'1'的'float *'转换为'GLfloat {aka float}'到'void glTranslatef(GLfloat,GLfloat,GLfloat)'

I have tried searching all over google but I couldn't find what I was looking for. 我已经尝试在整个Google上进行搜索,但是找不到我想要的东西。

glTranslatef takes three float arguments, not one array argument. glTranslatef接受三个float参数,而不是一个数组参数。 That's the end of it. 到此为止。

float XYZ[3] = {3,2,1};
glTranslatef(XYZ[0], XYZ[1], XYZ[2]);

If you're really desperate you can unpack it with a macro: 如果您真的很绝望,则可以使用宏将其解压缩:

#define UNPACK_TRI_ARRAY(ar) ar[0], ar[1], ar[2]

float XYZ[3] = {3,2,1};
glTranslatef(UNPACK_TRI_ARRAY(XYZ));

But once you get to that point, you have to ask yourself why . 但是一旦达到这一点,就必须问自己为什么

Try like this: 尝试这样:

glTranslatef(XYZ[0], XYZ[1], XYZ[2]);

http://www.opengl.org/sdk/docs/man/xhtml/glTranslate.xml http://www.opengl.org/sdk/docs/man/xhtml/glTranslate.xml

Prototype: 原型:

void glTranslatef(GLfloat   x,
                  GLfloat   y,
                  GLfloat   z);
glTranslatef(XYZ[0], XYZ[1], XYZ[2]);

AFAIK,我只是为使用const float *的函数定义一个重载,然后将数组元素传递给原始的三参数(X,Y,Z)函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM