【发布时间】:2015-11-19 12:13:43
【问题描述】:
我正在构建一个包含 c++ 程序的 R 包。检查运行良好,但我收到此消息 : 警告:ISO C++ 禁止变长数组‘s1’ [-Wvla]
CRAN 的维护人员说错误出在这部分代码中,如下所示。我认为“nrows”这个论点是多余的,但我想知道是否有另一种方法来解决这个问题
double entCI(double input[], int cMatrix[], double partition,
int nrows, int begin, int end)
{
double s1[nrows], s2[nrows], entropy;
int cs1[nrows], cs2[nrows];
int s1Count=0, s2Count=0, sCount=0;
while(input[begin]<partition)
{
cs1[s1Count]=cMatrix[begin];
s1[s1Count++]=input[begin++];
}
while(begin<end)
{
cs2[s2Count]=cMatrix[begin];
s2[s2Count++]=input[begin++];
}
sCount=s1Count+s2Count;
entropy=(s1Count/double(sCount))*ent(s1,cs1,s1Count)
+(s2Count/double(sCount))*ent(s2,cs2,s2Count);
return entropy;
}
【问题讨论】:
-
你的目标是什么?让它起作用?或者让它可移植正确的C++?使其工作的简单选择是禁用或忽略警告。该代码使用了在 C++ 中无效的 C99 功能,但您的 C++ 编译器支持该功能。要使其成为有效的可移植代码,您应该将本地数组更改为 unique_ptr 拥有的动态数组。
-
@JSF 这应该是一个答案。
标签: c++