不能用变量设置变量长度 [英] Can't set variable length with variable

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

问题描述

我现在想做的是创建一个长度由变量定义的数组。但是,当我把变量在数组长度,它给我一个变长数组的非POD元素类型'glm :: vec2'错误。但是,如果我用实际数字替换变量,错误消失。为什么会发生这种情况,我该如何解决?

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


推荐答案

在标准C ++中具有可变长度数组(VLA)

C ++标准不允许使用可变长度数组。在C ++中,数组的长度需要是编译时常量。一些编译器支持VLA作为编译器扩展,但是使用它们使得你的代码在其他编译器中不可移植。

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.

您可以使用 std :: vector 而不是VLA。

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

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

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