初始化元素不是编译时常量 c
Initializer element is not a compile-time constant c
我不知道为什么会收到此错误,但我的代码不符合要求并抛出错误
Initializer element is not a compile-time constant
这是代码
#include <math.h>
#include <stdio.h>
const float near = 0.1f;
const int fov_angle = 90;
const float far = 1000.0f;
const float width = 800.0f;
const float height = 600.0f;
const float aspect_ratio = width / height;
const float fov = (1.0f / tan(fov_angle / 2.0)); <- error
float projMat[4][4] = {
{fov * aspect_ratio, 0, 0, 0}, <- error
{0, fov, 0, 0},
{0, 0, ((far+near)/(far-near)), 1},
{0, 0, ((2*near*far)/(near-far)), 0}
};
如错误消息所述,fov
的初始值设定项是一个全局变量,无法在编译时计算,因为它包含函数调用。可执行代码不得驻留在任何函数之外。 projMat
有类似的问题,因为它依赖于 fov
.
您需要在函数内设置这些变量的值,可能 main
。
我不知道为什么会收到此错误,但我的代码不符合要求并抛出错误
Initializer element is not a compile-time constant
这是代码
#include <math.h>
#include <stdio.h>
const float near = 0.1f;
const int fov_angle = 90;
const float far = 1000.0f;
const float width = 800.0f;
const float height = 600.0f;
const float aspect_ratio = width / height;
const float fov = (1.0f / tan(fov_angle / 2.0)); <- error
float projMat[4][4] = {
{fov * aspect_ratio, 0, 0, 0}, <- error
{0, fov, 0, 0},
{0, 0, ((far+near)/(far-near)), 1},
{0, 0, ((2*near*far)/(near-far)), 0}
};
如错误消息所述,fov
的初始值设定项是一个全局变量,无法在编译时计算,因为它包含函数调用。可执行代码不得驻留在任何函数之外。 projMat
有类似的问题,因为它依赖于 fov
.
您需要在函数内设置这些变量的值,可能 main
。