Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 构造对象期间动态数组分配,非静态变量使用无效 类数组{ 公众: 数组(无符号h,无符号l,std::initializer\u list); .... 私人: 无符号长度; 双*阵列; ... }; 数组::数组(无符号l,std::初始值设定项\u列表il): 长度(l) { 数组(新的(std::nothrow)数组[l]); if(!数组) 扔掉它们(); 对于(auto el=il.begin(),int i=0;i_C++_Arrays_Dynamic_Allocation - Fatal编程技术网

C++ 构造对象期间动态数组分配,非静态变量使用无效 类数组{ 公众: 数组(无符号h,无符号l,std::initializer\u list); .... 私人: 无符号长度; 双*阵列; ... }; 数组::数组(无符号l,std::初始值设定项\u列表il): 长度(l) { 数组(新的(std::nothrow)数组[l]); if(!数组) 扔掉它们(); 对于(auto el=il.begin(),int i=0;i

C++ 构造对象期间动态数组分配,非静态变量使用无效 类数组{ 公众: 数组(无符号h,无符号l,std::initializer\u list); .... 私人: 无符号长度; 双*阵列; ... }; 数组::数组(无符号l,std::初始值设定项\u列表il): 长度(l) { 数组(新的(std::nothrow)数组[l]); if(!数组) 扔掉它们(); 对于(auto el=il.begin(),int i=0;i,c++,arrays,dynamic,allocation,C++,Arrays,Dynamic,Allocation,这只是我必须为我的作业做的原始课程的草稿。编译错误结果: 非静态数据成员“长度”的使用无效 无符号长度 有人知道如何解决这个问题吗?关于: class Array { public: Array(unsigned h, unsigned l, std::initializer_list<double>); .... private: unsigned length;

这只是我必须为我的作业做的原始课程的草稿。编译错误结果:

非静态数据成员“长度”的使用无效 无符号长度

有人知道如何解决这个问题吗?

关于:

class Array {                                           

public:

    Array(unsigned h, unsigned l, std::initializer_list<double>);
    ....
private:
    unsigned length;
    double * array;
...
};

Array::Array(unsigned l, std::initializer_list<double> il) :
length(l)
{
    array(new (std::nothrow) array[l]);
    if (!array)
        throw OutOfMem();
    for (auto el = il.begin(), int i = 0; i < length; i++)
    {
            if (el === il.end())
                throw WrongDim();
            array[i] = *el;
            el++;
        }
    }
}

这是相当混乱的代码,你有。您缺少了ctor定义中的
h
,因此编译器可能没有将其作为ctor进行解析,而是有点奇怪。其次,
array
的初始化可能也应该在mem初始值设定项列表中,您可能指的是
new(std::nothrow)double[l]
。当您发布有关错误的问题时,请始终在问题中包含完整的错误日志,完整的错误日志包含超过6页,我可以观察到大部分错误都是由于错误分配造成的。这就是为什么我要问这件事。问题是我不知道为什么我总是要初始化那些值,我不能用它们来分配构造对象中的动态数组。请解释一下这段代码如何帮助OP和这个线程的其他读者解决这个问题。有关回答指南的更多信息,请访问:
int i=0;
for (auto &el : il)
    array[i++] = el;

if (i!=l) 
    throw WrongDim();