我已经做这个问题好几个小时了,所以答案可能很明显,但我错过了,但我不断收到错误: 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/