error C2099: initializer is not a constant
2010-04-08 16:05
465 查看
in file main.c there is a code.
here is the code:
The initialization value must be a compile time constant. a is a
variable. Even if you defined a as const short, it is still not a
compile time constant.
解决方案
Your options for compile time constant initialization goes beyond hard-coded
values: You may initialize it to some macro's value (#define BOOOO_HISSSS)
or a specific enum value. The enum hackonly works for int.
With variables you can of course get around it by simply assigning after
declaring(instead of all-in-one initialization). The real problem rears it's
ugly head when you want to inizialize a const with somevalue based another
const.
Are there any other ways besides setting them to a macro or enum?
i.e.
------------------msdn上的样例和解释---------------------
C2099 can also occur because the compiler is not able to perform constant folding on an expression under /fp:strict because the floating point precision environment settings (see _controlfp_s for more information) may differ from compile to run time.
When constant folding fails, the compiler invokes dynamic initialization, which is not allowed in C.
To resolve this error, compile the module as a .cpp file or simplify the expression.
For more information, see /fp (Specify Floating-Point Behavior).
或者
This error is issued only by the C compiler and occurs only for non-automatic variables. The compiler initializes non-automatic variables at the start of the program and the values they are initialized with must be constant.
或者
here is the code:
short a=5; short b=a; void main() { }
The initialization value must be a compile time constant. a is a
variable. Even if you defined a as const short, it is still not a
compile time constant.
解决方案
Your options for compile time constant initialization goes beyond hard-coded
values: You may initialize it to some macro's value (#define BOOOO_HISSSS)
or a specific enum value. The enum hackonly works for int.
With variables you can of course get around it by simply assigning after
declaring(instead of all-in-one initialization). The real problem rears it's
ugly head when you want to inizialize a const with somevalue based another
const.
Are there any other ways besides setting them to a macro or enum?
i.e.
const int i = 1; const int j = i + 100 // error C2099 enum Name_Does_Not_Matter { bar = 5 }; const int foo = bar + 10 // Success!! foo is initialized to 15
------------------msdn上的样例和解释---------------------
C2099 can also occur because the compiler is not able to perform constant folding on an expression under /fp:strict because the floating point precision environment settings (see _controlfp_s for more information) may differ from compile to run time.
When constant folding fails, the compiler invokes dynamic initialization, which is not allowed in C.
To resolve this error, compile the module as a .cpp file or simplify the expression.
For more information, see /fp (Specify Floating-Point Behavior).
// C2099_2.c // compile with: /fp:strict /c float X = 2.0 - 1.0; // C2099 float X2 = 1.0; // OK
或者
This error is issued only by the C compiler and occurs only for non-automatic variables. The compiler initializes non-automatic variables at the start of the program and the values they are initialized with must be constant.
// C2099.c int j; int *p; j = *p; // C2099 *p is not a constant
或者
int i, j; int *p; j = i(); // error, i() is not constant j = *p; // error, *p is not a constant
相关文章推荐
- error C2099: initializer is not a constant 或者error C2099:初始值设定项不是常量
- error C2099: initializer is not a constant
- error C2099: initializer is not a constant
- 寒假软设--小型数据库进展(1)----error C2099: initializer is not a constant 或者error C2099:初始值设定项不是常量
- initializer element is not constant 问题
- rbtree.c:6: error: initializer element is not constant
- initializer element is not constant
- initializer element is not constant 问题
- error: initializer element is not constant
- error:“initializer element is not constant”
- 关于全局变量及静态变量和initializer element is not constant问题
- initializer element is not constant
- 结构体初始化 initializer element is not constant
- C语言:initializer element is not constant
- initializer element is not constant
- initializer element is not constant 问题
- [问题记录]编译时出现 initializer element is not constant 错误
- C C++全局变量初始化 initializer element is not constant 错误
- initializer element is not constant 问题
- 为什么出现“initializer element is not constant”错误