如何解决使用list <int>数组初始化共享指针时出错
我正在尝试使用shared_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::list<int>[]> adj;
};
当我尝试在构造函数中初始化如下所示的adjList时,出现编译错误。
Graph::Graph(int V)
{
this->V = V;
adj = std::make_shared<std::list<int>[]>(new std::list<int>[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>*)’
我在做什么错了?
解决方法
C ++ 11仅具有一个make_shared
函数。其原型如下所示:
template< class T,class... Args >
shared_ptr<T> make_shared( Args&&... args );
-
T
不能是数组。 - 该函数接受将传递给
T
构造函数的参数,但是std::list
没有可以接受new std::list<int>[V]
的构造函数。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。