我正在处理大量代码,并且想在参数文件中进行一些计算,因此我使用的是预处理器语言。
所以
const D_REAL spher2car[3] = {
sin(theta)*cos(phi),
sin(theta)*sin(phi),
cos(theta)
};
const D_REAL spher2car2[3] = {
spher2car[0]+h0*sin(thetap)*cos(phip),
spher2car[1]+h0*sin(thetap)*sin(phip),
spher2car[2]+h0*cos(thetap)
};
#if (spher2car[2]<spher2car2[2])
给我错误
the token "[" is not valid in preprocessor expressions
因此,基本上,我必须设置一个不再更改的值,但是该值是程序启动之前其他常量参数的依赖。
我该怎么做?
答案 0 :(得分:0)
预处理器甚至在编译器之前运行。您所拥有的只能在运行时进行评估。因此,请改用常规的if
语句:
if (spher2car[2]<spher2car2[2]) {
// do something
} else {
// do something else
}
请注意,因为这是必须的可执行代码,所以必须驻留在函数内部。上面的变量声明也是如此,因为必须对表达式进行求值以初始化它们。
执行此操作之前,最好先在main
中进行此操作。