【问题标题】:Initializer element is not a compile-time constant cInitializer 元素不是编译时常量 c
【发布时间】:2022-01-10 04:30:04
【问题描述】:

我不知道为什么会出现此错误,但我的代码不符合并引发错误 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}
};


【问题讨论】:

    标签: arrays c compiler-errors


    【解决方案1】:

    正如错误消息所述,fov 的初始化程序是一个全局变量,无法在编译时计算,因为它包含一个函数调用。可执行代码不能驻留在任何函数之外。 projMat 也有类似的问题,因为它依赖于fov

    您需要在函数内设置这些变量的值,可能是main

    【讨论】:

      猜你喜欢
      • 2014-08-04
      • 2015-12-11
      • 1970-01-01
      • 2013-03-16
      • 1970-01-01
      • 1970-01-01
      • 2011-09-02
      • 2014-02-18
      相关资源
      最近更新 更多