表达式在 C++ 中必须有一个常量值错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7488039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
expression must have a constant value error in c++
提问by y3di
Possible Duplicate:
Is there a way to initialize an array with non-constant variables? (C++)
可能的重复:
有没有办法用非常量变量初始化数组?(C++)
I have the following code:
我有以下代码:
vector<vector<vec2>> vinciP;
int myLines = -1;
myLines = drawPolyLineFile("vinci.dat", vinciP);
if (myLines > -1)
{
cout << "\n\nSUCCESS";
vec2 vPoints[myLines];
for (int i = 0; i < NumPoints; ++i)
{
vPoints[i] = vinciP[0][i];
}
}
I'm getting an error on the line 'vec2 vPoints[myLines];' that says expressions must have a constant value. I don't understand why I'm getting this error, any help?
我在“vec2 vPoints[myLines];”行收到错误 也就是说表达式必须有一个常量值。我不明白为什么我会收到这个错误,有什么帮助吗?
Is it because myLines could be negative? idk.
是因为 myLines 可能是负数吗?身。
回答by Nawaz
vec2 vPoints[myLines];
Since myLines
is not a constexpression ((which means, it is not known at compile-time), so the above code declares a variable length array which is not allowed in C++. Only C99 has this feature. Your compiler might have this as an extension (but that is not Standard C++).
由于myLines
不是const表达式((这意味着,它在编译时未知),因此上面的代码声明了一个在 C++ 中不允许的变长数组。只有 C99 具有此功能。您的编译器可能会将其作为扩展(但这不是标准 C++)。
The solution to such commom problem is : use std::vector<T>
as:
这种常见问题的解决方案是:std::vector<T>
用作:
std::vector<vec2> vPoints(myLines);
It should work now.
它现在应该可以工作了。
回答by Alok Save
Is it because myLines could be negative?
No, It is because myLines
is not a compile time constant.
是因为 myLines 可能是负数吗?
不,这是因为myLines
它不是编译时常量。
Explanation:
解释:
vec2 vPoints[myLines];
Creates an array of variable length, where myLines
value will be determined at Run-time.
Variable length arraysare not allowed in C++. It was a feature introduced in C99, and C++ Standard does not support it. Some C++ compilers support it as an extension though but it is nevertheless non standard conforming.
创建一个可变长度的数组,其中myLines
值将在运行时确定。
C++ 中不允许使用变长数组。这是 C99 中引入的一个特性,C++ 标准不支持它。一些 C++ 编译器虽然支持它作为扩展,但它仍然不符合标准。
For C++ size of an array should be known at compile time and hence must be compile time constant. myLines
is not a compile time constant and hence the error.
对于 C++ 数组的大小应该在编译时知道,因此必须是编译时常量。myLines
不是编译时间常数,因此是错误。
You should use a std::vector
你应该使用std::vector
回答by Mahesh
vec2 vPoints[myLines];
Array size mustbe a compile time constant. myLines
is not a compile time constant. Instead, allocate the memory using newor even better to use std::vector
.
数组大小必须是编译时常量。myLines
不是编译时常量。相反,使用new分配内存,甚至更好地使用std::vector
.
回答by R. Martinho Fernandes
C++ does not have variable-length arrays. The size of an array mustbe determined at compile-time. The value of myLines
is only known at runtime, so this won't work.
C++ 没有变长数组。数组的大小必须在编译时确定。的值myLines
仅在运行时才知道,所以这行不通。
To have arrays whose size is only known at runtime, use std::vector
.
要拥有仅在运行时才知道大小的数组,请使用std::vector
.
std::vector<vec2> vPoints(myLines);
回答by K-ballo
You are getting that error because static arrays need a static (constant) size. Since the number of components in your vPoints
is dynamic, consider using a dynamic array instead. Or better yet stick with vector
.
您收到该错误是因为静态数组需要静态(常量)大小。由于您的组件数量vPoints
是动态的,请考虑改用动态数组。或者更好的是坚持使用vector
.