期望的常量表达式,不能分配常量大小为0的数组 [英] Expected constant expression, cannot allocate an array of constant size 0

查看:1559
本文介绍了期望的常量表达式,不能分配常量大小为0的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个对象数组,如果我手动输入数组大小工作正常,但每当它试图从函数args获得信息时,它给出错误期望常量表达式和不能分配一个常量大小为0的数组。



我已经搜索过多个解决方案,但没有一个适用于我。代码目前看起来像这样:

  Array :: Array(int in,int min,int max)
{
size = in;
Heltal * htal [size];
}

大小整数和htal对象在头文件中定义。



似乎导致问题的行是最后一行。

解决方案

在C ++中不支持VLAs(可变长度数组) 。即使他们是 a 是一个矢量所以 Heltal * htal [a]; 没有意义。



使用 std :: vector 而不是一个有效大小的数组:

  std :: vector< Heltal *> htal(a.size()/ *或任何你想要的大小* /); 


I'm trying to create an object array and it works just fine if I enter the array size manually, but whenever it tries to get the infromation from the function args it gives the errors "Expected constant expression" and "cannot allocate an array of constant size 0".

I've searched around and found multiple solutions but none of them works for me. Code currently looks like this:

Array::Array(int in, int min, int max) 
{
    size = in;
    Heltal *htal[size];
}

The size integer and htal object is defined in the header file.

The line that seems to be causing the problem is the last one. Whats causing this and how do I fix it?

解决方案

VLAs (variable length arrays) are not supported in C++. Even if they were, a is a vector so Heltal *htal[a]; doesn't make sense.

You fix this by using an std::vector instead of an array (of a valid size):

std::vector<Heltal*> htal(a.size() /*or whatever size you want*/);

这篇关于期望的常量表达式,不能分配常量大小为0的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆