在类里面
我有一个公共(public)属性TissueCompartments:
class Diver {
public:
TissueCompartments tissueCompartments[16];
Diver();
Diver(const Diver& orig, TissueCompartments tissueCompartments[16]);
virtual ~Diver();
};
我有一个构造函数
Diver::Diver(const Diver& orig, TissueCompartments tissueCompartments[16]) {
this->tissueCompartments=tissueCompartments;
}
我收到此错误:
错误:将“TissueCompartments *”分配给“TissueCompartments [16]”时类型不兼容
最佳答案
数组不可分配。而是在构造函数主体中执行以下操作:
#include <algorithm>
//..
Diver::Diver(const Diver& orig, TissueCompartments* tC)
{
std::copy(tC, tC + 16, tissueCompartments);
//...
}
另外,请注意此声明:
Diver::Diver(const Diver& orig, TissueCompartments tissueCompartments[16])
没什么不同:
Diver::Diver(const Diver& orig, TissueCompartments* tissueCompartments)
即使将
[16]
放在参数中,数组也会衰减到指针。因此[16]
绝对无效(除了有助于让您知道它是被引用的16数组之外)。另请注意,在进行任何这些更改之前,
TissueCompartment
类型需要为可分配的类型。