首页 > 解决方案 > 说它不能用作常数

问题描述

视觉工作室 2019

const int inputs = 1, layers = 2, layerN = 3, output = 1;
const int how = inputs * layerN + pow(layerN,layers) + output * layerN;
float w[how];

它说它w[how]必须是“const”表达式(但是是??)

我无法运行此错误的程序。

帮助。

标签: c++

解决方案


how不是常量表达式。它的值在编译时不为编译器所知,它是在运行时动态计算的,因为函数调用pow(). 因此,它不能用于声明固定长度的数组。您将不得不使用new[]orstd::vector代替:

float *w = new float[how];
...
delete[] w;
#include <vector>
std::vector<float> w(how);

推荐阅读