0

I'm trying to implement a Graph class with shared_ptr. I've declared my class as below.

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;
};

When I try to initialize my adjList like below in the constructor, I'm getting a compilation error.

Graph::Graph(int V)
{
    this->V = V;
    adj = std::make_shared<std::list<int>[]>(new std::list<int>[V]);
}

Error:

/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>*)’

What am I doing wrong ?

2
  • Rethink your approach. Why to you want a std::shared_ptr to an array of std::list? Consider using a std::vector of std::lists as a replacement. Commented Oct 30, 2020 at 16:44
  • 1
    C++11 has only one make_shared function. Commented Oct 30, 2020 at 16:45

1 Answer 1

2

C++11 has only one make_shared function. This is what its prototype looks like:

template< class T, class... Args >
shared_ptr<T> make_shared( Args&&... args );
  1. T can't be an array.
  2. The function accepts arguments that will be passed to the T constructor, but std::list has no constructor that can accept new std::list<int>[V].
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.