C++ 使用列表数组初始化共享指针时出错<;int>;
我正在尝试用共享的ptr实现一个Graph类。我宣布我的班级如下C++ 使用列表数组初始化共享指针时出错<;int>;,c++,c++11,shared-ptr,C++,C++11,Shared Ptr,我正在尝试用共享的ptr实现一个Graph类。我宣布我的班级如下 class Graph { public: Graph(int V); Graph()=delete; void AddEdge(int src, int dest); void BFS(int s); ~Graph(); private: int V; std::shared_ptr<std::l
class Graph
{
public:
Graph(int V);
Graph()=delete;
void AddEdge(int src, int dest);
void BFS(int s);
~Graph();
private:
int V;
std::shared_ptr<std::list<int>[]> adj;
};
类图
{
公众:
图形(INTV);
Graph()=删除;
无效补遗(int src,int dest);
无效BFS(int-s);
~Graph();
私人:
INTV;
标准:共享的;
};
当我尝试在构造函数中初始化我的adjList时,我得到了一个编译错误
Graph::Graph(int V)
{
this->V = V;
adj = std::make_shared<std::list<int>[]>(new std::list<int>[V]);
}
Graph::Graph(intv)
{
这个->V=V;
adj=std::使共享(新std::列表[V]);
}
错误:
/usr/include/c++/9/bits/shared_ptr.h:717:39: required from ‘std::shared_ptr<_Tp> std::make_shared(_Args&& ...) [with _Tp = std::__cxx11::list<int> []; _Args = {std::__cxx11::list<int, std::allocator<int> >*}]’
Graph.cpp:21:67: required from here
/usr/include/c++/9/ext/new_allocator.h:145:20: error: no matching function for call to ‘std::__cxx11::list<int>::list(std::__cxx11::list<int>*)’
/usr/include/c++/9/bits/shared_ptr.h:717:39:从'std::shared_ptr std::make_shared(_Args&&…[带_Tp=std::_cxx11::list[];_Args={std:_cxx11::list*}]中需要
图表cpp:21:67:此处需要
/usr/include/c++/9/ext/new_allocator.h:145:20:错误:调用“std::uucxx11::list::list(std:uuucxx11::list*)”时没有匹配的函数
我做错了什么?C++11只有一个函数。这就是它的原型:
template< class T, class... Args >
shared_ptr<T> make_shared( Args&&... args );
模板
共享\u ptr使\u共享(Args&…Args);
T
不能是数组T
构造函数的参数,但std::list
没有可以接受new std::list[V]
的构造函数template< class T, class... Args >
shared_ptr<T> make_shared( Args&&... args );
模板
共享\u ptr使\u共享(Args&…Args);
T
不能是数组T
构造函数的参数,但std::list
没有可以接受new std::list[V]
的构造函数重新思考你的方法。为什么要将
std::shared\u ptr
添加到std::list
的数组中?考虑使用<代码> STD:<代码>代码> STD::列表< /Cords>作为替换。C++ 11只有一个代码> MaxySuffy函数。重新考虑您的方法。为什么要将std::shared\u ptr
添加到std::list
的数组中?考虑使用<代码> STD::vector <代码> STD::列表< /代码> S作为替换。C++ 11只有一个代码> MaMuxApdio/<代码>函数。