为什么我得到关于初始化器不是常数的错误?

时间:2022-09-06 12:13:34

I am using the following code.

我使用以下代码。

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};

When I compile it, GCC gives me the following error.

当我编译它时,GCC给我以下错误。

Transformations.h:16:1: error: initializer element is not constant

Transformations.h:16:1:错误:初始化元素不是常量

What does that mean? How can I fix my code?

那是什么意思?我该如何修复我的代码?

6 个解决方案

#1


14  

You can't do this at global scope in C, only at local scope, i.e. within a function:

您不能在C中的全局范围内执行此操作,仅在本地范围内,即在函数内:

#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
}

Alternatively you could compile the code as C++ rather than C.

或者,您可以将代码编译为C ++而不是C.

#2


20  

Often people are mislead by the naming of the keyword const, implying something of a constant value that can't be changed. In C at least, it means readonly. const qualified objects at file scope are not having the proper constness to serve as array initializers.

通常人们会被关键字const的命名误导,这意味着某些不可改变的常量值。至少在C中,它意味着只读。文件范围内的const限定对象没有适当的const作为数组初始值设定项。

As an example for non-constant constness, it is perfectly ok to declare

作为非常量常量的一个例子,声明是完全可以的

 const volatile unsigned int milliseconds_since_boot;

being a value that gets updated from outside the compiler's control (think HW register) and that you are not allowed to assign to, i.e. it is readonly.

是一个从编译器控件外部更新的值(想想硬件寄存器),并且不允许分配给它,即它是只读的。

#3


6  

I'm not a proper programmer ;) but I'd do this:

我不是一个合适的程序员;)但我会这样做:

#define X_ORIGIN (1233086)
#define Y_ORIGIN (-4728071)
#define Z_ORIGIN (4085704)
const int xyzOrigin[NUM_DIMENSIONS] = {X_ORIGIN, Y_ORIGIN, Z_ORIGIN};

That way it's just a text-substitution. If the compiler still spits the dummy at least you're a step closer to knowing where the issue is.

这样它只是一个文本替换。如果编译器仍然至少吐出虚拟对象,那么距离知道问题的位置还有一步之遥。

#4


4  

As an alternative, this would also work in this case:

作为替代方案,这也适用于这种情况:

enum { X_ORIGIN = 1233086,
       Y_ORIGIN = -4728071,
       Z_ORIGIN = 4085704 };

const int xyzOrigin[] = { X_ORIGIN, Y_ORIGIN, Z_ORIGIN };

int main()
{
    return 0;
}

#5


2  

In C language objects with static storage duration has to be initialized with constant expressions or with aggregate initializers containing constant expressions. --Answer of AndreyT

在C语言中,具有静态存储持续时间的对象必须使用常量表达式或包含常量表达式的聚合初始化程序进行初始化。 - AndreyT的答案

After reading, You must have the knowledge that NUM_DIMENSIONS, If it has the const-qualification, isn't a constant! Then you can't initializate your array this way.

阅读之后,您必须知道NUM_DIMENSIONS,如果它具有const资格,则不是常数!那么你不能用这种方式初始化你的数组。

For use this code:

使用此代码:

const int xyzOrigin[NUM_DIMENSIONS] = {X_ORIGIN, Y_ORIGIN, Z_ORIGIN};

const int xyzOrigin [NUM_DIMENSIONS] = {X_ORIGIN,Y_ORIGIN,Z_ORIGIN};

You should use: #define NUM_DIMENSIONS 3 or you could just declare without any variable inside the square brackets const int xyzOrigin[] = {X_ORIGIN, Y_ORIGIN, Z_ORIGIN};

您应该使用:#define NUM_DIMENSIONS 3或者您可以在方括号内声明没有任何变量const int xyzOrigin [] = {X_ORIGIN,Y_ORIGIN,Z_ORIGIN};

#6


0  

As triclosan said:

正如三氯生所说:

main()
{
    const int X_ORIGIN = 1233086;
    const int Y_ORIGIN = -4728071;
    const int Z_ORIGIN = 4085704;
    const int xyzOrigin[] = {X_ORIGIN, Y_ORIGIN, Z_ORIGIN};
}

this works fine...

这很好......

or, if you know the dimensions beforehand, this:

或者,如果你事先知道尺寸,这个:

#define DIM 3

main()
{
    const int X_ORIGIN = 1233086;
    const int Y_ORIGIN = -4728071;
    const int Z_ORIGIN = 4085704;
    const int xyzOrigin[DIM] = {X_ORIGIN, Y_ORIGIN, Z_ORIGIN};
}

#1


14  

You can't do this at global scope in C, only at local scope, i.e. within a function:

您不能在C中的全局范围内执行此操作,仅在本地范围内,即在函数内:

#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
}

Alternatively you could compile the code as C++ rather than C.

或者,您可以将代码编译为C ++而不是C.

#2


20  

Often people are mislead by the naming of the keyword const, implying something of a constant value that can't be changed. In C at least, it means readonly. const qualified objects at file scope are not having the proper constness to serve as array initializers.

通常人们会被关键字const的命名误导,这意味着某些不可改变的常量值。至少在C中,它意味着只读。文件范围内的const限定对象没有适当的const作为数组初始值设定项。

As an example for non-constant constness, it is perfectly ok to declare

作为非常量常量的一个例子,声明是完全可以的

 const volatile unsigned int milliseconds_since_boot;

being a value that gets updated from outside the compiler's control (think HW register) and that you are not allowed to assign to, i.e. it is readonly.

是一个从编译器控件外部更新的值(想想硬件寄存器),并且不允许分配给它,即它是只读的。

#3


6  

I'm not a proper programmer ;) but I'd do this:

我不是一个合适的程序员;)但我会这样做:

#define X_ORIGIN (1233086)
#define Y_ORIGIN (-4728071)
#define Z_ORIGIN (4085704)
const int xyzOrigin[NUM_DIMENSIONS] = {X_ORIGIN, Y_ORIGIN, Z_ORIGIN};

That way it's just a text-substitution. If the compiler still spits the dummy at least you're a step closer to knowing where the issue is.

这样它只是一个文本替换。如果编译器仍然至少吐出虚拟对象,那么距离知道问题的位置还有一步之遥。

#4


4  

As an alternative, this would also work in this case:

作为替代方案,这也适用于这种情况:

enum { X_ORIGIN = 1233086,
       Y_ORIGIN = -4728071,
       Z_ORIGIN = 4085704 };

const int xyzOrigin[] = { X_ORIGIN, Y_ORIGIN, Z_ORIGIN };

int main()
{
    return 0;
}

#5


2  

In C language objects with static storage duration has to be initialized with constant expressions or with aggregate initializers containing constant expressions. --Answer of AndreyT

在C语言中,具有静态存储持续时间的对象必须使用常量表达式或包含常量表达式的聚合初始化程序进行初始化。 - AndreyT的答案

After reading, You must have the knowledge that NUM_DIMENSIONS, If it has the const-qualification, isn't a constant! Then you can't initializate your array this way.

阅读之后,您必须知道NUM_DIMENSIONS,如果它具有const资格,则不是常数!那么你不能用这种方式初始化你的数组。

For use this code:

使用此代码:

const int xyzOrigin[NUM_DIMENSIONS] = {X_ORIGIN, Y_ORIGIN, Z_ORIGIN};

const int xyzOrigin [NUM_DIMENSIONS] = {X_ORIGIN,Y_ORIGIN,Z_ORIGIN};

You should use: #define NUM_DIMENSIONS 3 or you could just declare without any variable inside the square brackets const int xyzOrigin[] = {X_ORIGIN, Y_ORIGIN, Z_ORIGIN};

您应该使用:#define NUM_DIMENSIONS 3或者您可以在方括号内声明没有任何变量const int xyzOrigin [] = {X_ORIGIN,Y_ORIGIN,Z_ORIGIN};

#6


0  

As triclosan said:

正如三氯生所说:

main()
{
    const int X_ORIGIN = 1233086;
    const int Y_ORIGIN = -4728071;
    const int Z_ORIGIN = 4085704;
    const int xyzOrigin[] = {X_ORIGIN, Y_ORIGIN, Z_ORIGIN};
}

this works fine...

这很好......

or, if you know the dimensions beforehand, this:

或者,如果你事先知道尺寸,这个:

#define DIM 3

main()
{
    const int X_ORIGIN = 1233086;
    const int Y_ORIGIN = -4728071;
    const int Z_ORIGIN = 4085704;
    const int xyzOrigin[DIM] = {X_ORIGIN, Y_ORIGIN, Z_ORIGIN};
}