无法设置可变长度可变 [英] Can't set variable length with variable

查看:225
本文介绍了无法设置可变长度可变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图现在要做的是建立与被界定可变长度的数组。然而,当我把变量数组的长度,它给我的错误非POD元素类型GLM :: VEC2'变长数组。但是,如果我有一个实际的数字替换变量,错误消失。为什么会出现这种情况,我该如何解决这个问题?

 
INT numtriangles = sector1.numtriangles;
GLM :: VEC2 TEX [测试] //使用一个变量产生一个错误
GLM :: VEC3顶点[10]; //这里没有错误


解决方案

您不能有变长数组(VLA)标准C ++。结果
变长数组不是由C ++标准允许的。在C ++中的数组的长度必须是一个编译时间常数。有些编译器都支持VLA作为一个编译器扩展,但使用起来使得在其他的编译器的code不可移植。

您可以使用, 的std ::矢量 而不是VLA的。

What I'm trying to do right now is to create an array with a length that is defined by a variable. However, when I put the variable in the array length, it gives me a "Variable length array of non-POD element type 'glm::vec2'" error. However, if I replace the variable with an actual number, the error goes away. Why does this happen and how can I fix this?


int numtriangles = sector1.numtriangles;


glm::vec2 tex[test]; //Using a variable generates an error
glm::vec3 vertices[10]; //No error here

解决方案

You cannot have variable length arrays(VLA) in standard C++.
Variable length arrays are not allowed by the C++ Standard. In C++ the length of the array needs to be a compile time constant. Some compilers do support VLA as a compiler extension, but using them makes your code non-portable across other compilers.

You can use, std::vector instead of an VLA.

这篇关于无法设置可变长度可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆