This is a .h file to store formulas but it gives me the ERROR you see at the tittle on line #16(below the string). I'm a beginner and the error says is not constant so I though of putting "const" before the "float" but it doesn't work either way.
这是一个用于存储公式的.h文件,但是它给出了您在第16行(在字符串下面)看到的错误。我是初学者,错误说的不是常量,所以我把“const”放在“float”之前,但不管用哪种方式都不行。
#ifndef FORMULAS_H_INCLUDED
#define FORMULAS_H_INCLUDED
float a;
float Pi=3.1415926536;
float r;
float b;
float c;
float h;
char formula_volume_cube[100] = "Formula for the Volume of a Cube: V=a*a*a";
const float volume_cube=(a*a*a);
#endif // FORMULAS_H_INCLUDED
Any help will be appreciated. THANK you for YOUR time.
如有任何帮助,我们将不胜感激。谢谢你抽出时间来。
2 个解决方案
#1
3
Global variables in C can only be initialized with compile-time constant expressions, and that is a very narrow class of expressions. It essentially only comprises literals or expressions involving literals:
C中的全局变量只能用编译时常量表达式初始化,这是一个非常狭窄的表达式类。它实际上只包含了包含文字的文字或表达式:
float a1 = 1.5; // OK
const float a2 = 2.5 * 10 / 300; // Also OK
An expression that identifies another global variable is not a compile-time constant expression:
标识另一个全局变量的表达式不是编译时常量表达式:
// float a3 = a2; // Error!
C is just not expressive enough for this. This is all for the sake of simplicity. For example, C does not distinguish between const float b1 = 2.5;
and extern const float b2;
. The variable b1
is no "more const" than b2
, but only b1
's value is known to the compiler, whereas b2
's value is not known — it may be set only by a different translation unit, and without any ordering. So there's no way to make one constant "more constant" than the other. The simple solution in C is that no id-expression can be used as a compile-time constant.
C只是不够表达。这都是为了简单起见。例如,C不区分const float b1 = 2.5;和外部的const float b2;。变量b1并不比b2更“const”,但是编译器只知道b1的值,而b2的值是未知的——它只能由一个不同的翻译单元来设置,并且没有任何排序。所以没有办法让一个常数比另一个常数“更稳定”。在C中简单的解是没有任何的id表达式可以作为编译时常量。
(C++, by contrast, allows global variables to have dynamic initializers. This introduces a whole set of ordering concerns if one global variable's initializer depends on another global variable. C has none of those problems.)
相比之下,c++允许全局变量具有动态初始化器。如果一个全局变量的初始化器依赖于另一个全局变量,那么就会引发一系列的排序问题。C没有这些问题。
#2
0
The message
的消息
Error: Initializer Element is not constant
错误:初始化元素不是常量。
pretty much says it all
说得差不多了。
float a;
is not declared as a constant (it can change as the program runs), so
没有被声明为常量(它可以随着程序的运行而改变),那么?
const float volume_cube=(a*a*a);
cannot be a constant, as it is based on the non-constant a
.
不可能是常数,因为它是基于非常数a的。
#1
3
Global variables in C can only be initialized with compile-time constant expressions, and that is a very narrow class of expressions. It essentially only comprises literals or expressions involving literals:
C中的全局变量只能用编译时常量表达式初始化,这是一个非常狭窄的表达式类。它实际上只包含了包含文字的文字或表达式:
float a1 = 1.5; // OK
const float a2 = 2.5 * 10 / 300; // Also OK
An expression that identifies another global variable is not a compile-time constant expression:
标识另一个全局变量的表达式不是编译时常量表达式:
// float a3 = a2; // Error!
C is just not expressive enough for this. This is all for the sake of simplicity. For example, C does not distinguish between const float b1 = 2.5;
and extern const float b2;
. The variable b1
is no "more const" than b2
, but only b1
's value is known to the compiler, whereas b2
's value is not known — it may be set only by a different translation unit, and without any ordering. So there's no way to make one constant "more constant" than the other. The simple solution in C is that no id-expression can be used as a compile-time constant.
C只是不够表达。这都是为了简单起见。例如,C不区分const float b1 = 2.5;和外部的const float b2;。变量b1并不比b2更“const”,但是编译器只知道b1的值,而b2的值是未知的——它只能由一个不同的翻译单元来设置,并且没有任何排序。所以没有办法让一个常数比另一个常数“更稳定”。在C中简单的解是没有任何的id表达式可以作为编译时常量。
(C++, by contrast, allows global variables to have dynamic initializers. This introduces a whole set of ordering concerns if one global variable's initializer depends on another global variable. C has none of those problems.)
相比之下,c++允许全局变量具有动态初始化器。如果一个全局变量的初始化器依赖于另一个全局变量,那么就会引发一系列的排序问题。C没有这些问题。
#2
0
The message
的消息
Error: Initializer Element is not constant
错误:初始化元素不是常量。
pretty much says it all
说得差不多了。
float a;
is not declared as a constant (it can change as the program runs), so
没有被声明为常量(它可以随着程序的运行而改变),那么?
const float volume_cube=(a*a*a);
cannot be a constant, as it is based on the non-constant a
.
不可能是常数,因为它是基于非常数a的。