Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用列表数组初始化共享指针时出错<;int>;_C++_C++11_Shared Ptr - Fatal编程技术网

C++ 使用列表数组初始化共享指针时出错<;int>;

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

我正在尝试用共享的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;
};
类图
{
公众:
图形(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]
    的构造函数
  • 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]
    的构造函数

  • 重新思考你的方法。为什么要将
    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/<代码>函数。