在我的头文件中,我有以下代码
class ExEvent : public Event {
public:
ExEvent(
Item* dst[SIZE],
);
~ExEvent();
Item* dst[SIZE];
};
在cpp文件中,我有如下代码
ExEvent::ExEvent(
Item * dst[SIZE],
) : Event() {
this->dst = &dst;
}
我收到以下错误:
error: array type 'Item *[15]' is not assignable
this->dst = &dst;
有人能解释为什么会发生这个错误以及为什么我不能将 dst 数组指针分配给 this->dst。
最佳答案
在函数参数中 type[any-size]
实际上是 type*
。 IE。 ExEvent(Item*[SIZE])
实际上是 ExEvent(Item**)
。
因此,修复代码:
ExEvent::ExEvent(Item* src[SIZE])
{
std::copy_n(src, SIZE, this->dst);
}
确保 src
有足够的元素。
参见 declaring functions: parameter list了解更多详情:
If the type is "array of T" or "array of unknown bound of T", it is replaced by the type "pointer to T"
关于c++ - 在 C++ 中将指针数组分配给私有(private)变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40745367/