为什么会收到有关初始化程序不是常量的错误? [英] Why do I get an error about the initializer not being a constant?
本文介绍了为什么会收到有关初始化程序不是常量的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码。
const int X_ORIGIN = 1233086;
const int Y_ORIGIN = -4728071;
const int Z_ORIGIN = 4085704;
const int xyzOrigin[NUM_DIMENSIONS] = {X_ORIGIN, Y_ORIGIN, Z_ORIGIN};
编译时,GCC给我以下错误。
When I compile it, GCC gives me the following error.
Transformations.h:16:1:错误:初始值设定项元素不是常量
Transformations.h:16:1: error: initializer element is not constant
那是什么意思?我该如何修复我的代码?
What does that mean? How can I fix my code?
推荐答案
您不能在C语言中的全局范围内执行此操作,只能在本地范围内执行此操作,即在函数内:
You can't do this at global scope in C, only at local scope, i.e. within a function:
#define NUM_DIMENSIONS 3
const int X_ORIGIN = 1233086;
const int Y_ORIGIN = -4728071;
const int Z_ORIGIN = 4085704;
const int xyzOrigin[NUM_DIMENSIONS] = {X_ORIGIN, Y_ORIGIN, Z_ORIGIN}; // FAIL
void foo(void)
{
const int xyzOrigin[NUM_DIMENSIONS] = {X_ORIGIN, Y_ORIGIN, Z_ORIGIN}; // OK
}
或者,您也可以将代码编译为C ++而不是C。
Alternatively you could compile the code as C++ rather than C.
这篇关于为什么会收到有关初始化程序不是常量的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文