shared_ptr类的向量的向量的初始化问题

我正在尝试初始化大小为19x19(_goban)的shared_ptr类的向量的向量。

class Goban
{
  public:
    Goban();
    ~Goban();
  private:
    vector<vector<shared_ptr<Cell>>> _goban;
};

我的构造函数是这样的:

Goban::Goban() : _goban(18,vector<make_shared<Cell>>(18,new Cell))
{
}

我找不到初始化方法。

我收到此错误:

template <class _Tp,class _Allocator /* = allocator<_Tp> */>

有什么想法吗?

lst0415 回答:shared_ptr类的向量的向量的初始化问题

您指定了错误的模板参数make_shared<Cell>,应为shared_ptr<Cell>。并且请注意,禁止从原始指针到std::shared_ptr的隐式转换。然后

Goban::Goban() : _goban(18,vector<shared_ptr<Cell>>(18,make_shared<Cell>()))
//                                 ^^^^^^^^^^^^^^^^      ^^^^^^^^^^^^^^^^^^^
{
}

借助deduction guide,您甚至可以省略将模板参数指定为

Goban::Goban() : _goban(18,vector(18,make_shared<Cell>()))
{
}
本文链接:https://www.f2er.com/3155700.html

大家都在问