c++ - 分配数组 - "cannot convert from double* to double[]"

标签 c++ arrays

我已经做这个问题好几个小时了,所以答案可能很明显,但我错过了,但我不断收到错误: C2440:“=”:无法从“double *”转换为“double[7]” IntelliSense:表达式必须是可修改的左值

这是有问题的代码:

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

class SnowData
{
private:
    int dates[7];
    double inches[7];
    string monthName;
    int startdate;
public:
    SnowData(string _monthName, int _startdate, double* _inches)
    {
        inches = _inches;
        monthName = _monthName;
        startdate = _startdate;
    }

我只需要从我的构造函数中获取 _inches 到我的 inches 数组中,我终于可以完成这个问题了。

最佳答案

正如@chris 所说,您不能分配数组。但是,您可以分配单个元素(前提是它们具有适当的 = 运算符)。

for(int i = 0; i < 7; ++i) {
    inches[i] = _inches[i];
}

这遍历 inches 并在 _inches 中为每个元素分配相应的 on。

警告:未定义的行为是 _inches 尺寸小于 7。

关于c++ - 分配数组 - "cannot convert from double* to double[]",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23122295/

相关文章:

c++ - 将数组作为函数参数传递时出错

c++ - Win32 CMutex 与标准库 std::mutex 的比较

c++ - 使用 C++ 从 bios 获取 Assets 标签和序列号

c - C中循环队列的数据结构

数组中 'in' 元素的 JavaScript `undefined` 运算符

对象数组中的javascript排序元素

c++ - C++ 'struct name' 中的结构未命名类型

c++ - 二维数组指针运算

c++ - Cuda C++ 设计 : reusable class with unknown compile-time size

ios - 在我的扩展代码中调用数组时使用未解析的标识符错误