gpt4 book ai didi

c++ - 使用构造函数初始化数组(C++)

转载 作者:行者123 更新时间:2023-11-30 00:52:09 26 4
gpt4 key购买 nike

如何通过构造函数初始化数组?下面是名为 Sort 的类的代码:

private:
double unpartitionedList[];

public:
Sort(double unpartitionedList[]);

Sort::Sort(double unpartitionedList[])
{
this->unpartitionedList = unpartitionedList;
}

我希望能够将数组传递给构造函数并将其存储在 unpartitionedList[] 中。像这样:Sort(array[])

按照现在的代码,我在 DevC++ 中遇到编译器错误:

"[Error] incompatible types in assignment of 'double*' to 'double[0]'"

我曾尝试在我认为需要的地方插入引用 (&) 和取消引用 (*) 运算符,但不幸的是,我的尝试没有成功。

如有任何建议,我们将不胜感激。

最佳答案

数组不可赋值。您必须按元素进行复制或编写实际的 C++ 代码并使用 std::arraystd::vector

关于c++ - 使用构造函数初始化数组(C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19759186/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com