(C ++)表达式必须具有恒定值

时间:2018-11-18 19:43:36

标签: c++

Visual Studio出于某种奇怪的原因向我显示此错误,并一直说在尝试初始化数组时count不是const int。检查图像。

Error: expression must have a constant value enter image description here

这是主函数调用:

std::string fileName("shows.tv");
const int COUNT = 10;
Episode** episodes = loadEpisodesFromFile(fileName, COUNT);

这是头文件中的函数声明:

Episode** loadEpisodesFromFile(std::string, const int);

我不明白。变量count已被声明为const int,但不起作用。

1 个答案:

答案 0 :(得分:1)

Episode* episodes[count]无效,因为 count 是函数中的参数 loadEpisodesFromFile ,在编译时未知...

您需要改用std :: vector

std::vector<Episode*> episodes(count);