Why does the compiler give me the following error message on the provided code: "initializer element is not constant". The corresponding C/C++ code compiles perfectly under gcc.
为什么编译器在提供的代码上给出了以下错误消息:“初始化元素不是常量”。相应的C / C ++代码在gcc下完美编译。
#import <Foundation/Foundation.h>
const float a = 1;
const float b = a + a; // <- error here
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
NSLog(@"Hello, World!");
[pool drain];
return 0;
}
3 个解决方案
#1
12
That code will only compile correctly if the const float
statements appear somewhere other than the file scope.
如果const float语句出现在文件范围之外的某个地方,那么该代码将只能正确编译。
It is part of the standard, apparently. It is important that all file-scope declared variables are initialised with constant expressions, not expressions involving constant variables.
显然,它是标准的一部分。重要的是所有文件范围声明的变量都使用常量表达式初始化,而不是涉及常量变量的表达式。
You are initialising the float 'b' with the value of another object. The value of any object, even if it is a const qualified, is not a constant expression in C.
您正在使用另一个对象的值初始化浮动'b'。任何对象的值,即使它是const限定的,也不是C中的常量表达式。
#2
4
@dreamlax is correct, you can't have a const declaration whose initialization depends upon another (const) variable. If you need one to depend on the other, I suggest creating a variable that you can treat as a constant and initialize it only once. See these SO questions for details:
@dreamlax是正确的,你不能有一个const声明,其初始化依赖于另一个(const)变量。如果你需要一个依赖另一个,我建议创建一个你可以视为常量的变量,并只初始化一次。有关详情,请参阅以下SO问题:
- Defining a constant in objective-c
- Constants in Objective C
在objective-c中定义一个常量
目标C中的常量
#3
1
I don't have Xcode on my machine here so I can't try my example,
我的机器上没有Xcode所以我不能尝试我的例子,
But can you try
但你能尝试吗?
#define A (1)
#define B (A + A)
const float a = A;
const float b = B;
#1
12
That code will only compile correctly if the const float
statements appear somewhere other than the file scope.
如果const float语句出现在文件范围之外的某个地方,那么该代码将只能正确编译。
It is part of the standard, apparently. It is important that all file-scope declared variables are initialised with constant expressions, not expressions involving constant variables.
显然,它是标准的一部分。重要的是所有文件范围声明的变量都使用常量表达式初始化,而不是涉及常量变量的表达式。
You are initialising the float 'b' with the value of another object. The value of any object, even if it is a const qualified, is not a constant expression in C.
您正在使用另一个对象的值初始化浮动'b'。任何对象的值,即使它是const限定的,也不是C中的常量表达式。
#2
4
@dreamlax is correct, you can't have a const declaration whose initialization depends upon another (const) variable. If you need one to depend on the other, I suggest creating a variable that you can treat as a constant and initialize it only once. See these SO questions for details:
@dreamlax是正确的,你不能有一个const声明,其初始化依赖于另一个(const)变量。如果你需要一个依赖另一个,我建议创建一个你可以视为常量的变量,并只初始化一次。有关详情,请参阅以下SO问题:
- Defining a constant in objective-c
- Constants in Objective C
在objective-c中定义一个常量
目标C中的常量
#3
1
I don't have Xcode on my machine here so I can't try my example,
我的机器上没有Xcode所以我不能尝试我的例子,
But can you try
但你能尝试吗?
#define A (1)
#define B (A + A)
const float a = A;
const float b = B;