目前,我有两个类(class),Ai
和 Action
.
我的类(class) Ai
获得成员(member)Action
称为 _root
.
class Ai
{
public:
Ai();
~Ai();
private:
Action _root;
};
当我想初始化我的类时出现问题Action
和我的构造函数 Ai
.
Ai::Ai() : _root(Action(0, AI, 0, 0))
{
}
我的类(class) Action
与我的构造函数:
class Action
{
public:
Action(int, Availability, int, int);
~Action();
private:
int _y;
int _x;
int _depth;
Availability _type;
vector<unique_ptr<Action>> _child;
};
Action::Action(int depth, Availability type, int y, int x)
{
this->_y = y;
this->_x = x;
this->_depth = depth;
this->_type = type;
}
问题恰恰发生在 vector<unique_ptr<Action>> _child;
上.
当我创建对象时 Ai
,我的构造函数创建了一个对象 Action
使用默认值,但它不能初始化 unique_ptr
的 vector .
我的调试器给我这个错误:
error: call to implicitly-deleted copy constructor of
'std::__1::unique_ptr<Action, std::__1::default_delete<Action> >'
::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
有什么想法吗?
最佳答案
您声明 Ai
构造函数的方式是创建一个临时 Action
对象,然后将其复制到 _root
.
解决方法很简单:
Ai::Ai() : _root(0, AI, 0, 0)
将构造函数参数直接传递给 _root
。
无关,你不需要使用this->
来访问Action::Action
中的成员变量。
关于c++ - 无法在类中初始化 unique_ptr 的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59119541/