0

I created a graph and added nodes and transitions to it in Lemon Graph Library

typedef ListDigraph Graph;
vector<Graph> Process;
for(temp = temp.child("role");temp;temp = temp.next_sibling("role"))
{
    Graph proc;
    for(xml_node temp1 = temp.child("states").child("state");temp1;temp1 = temp1.next_sibling())
    {
        string state = temp1.child_value();
        state = role + "_" + state;
        vertex = process.addNode();
        state_name[vertex] = state;
    }
    Process.push_back(proc);
}

If I don't push it in vector everything works fine, but when I try to push it in Process I get error. Error is 70-80 lines long, but the main point I found is:

/usr/local/include/lemon/list_graph.h:336:5: error:
lemon::ListDigraph::ListDigraph(const lemon::ListDigraph&) is private 

After doing what @Magtheridon96 suggested getting the following error:

    In file included from /usr/include/c++/4.6/i686-linux-gnu/./bits/c++allocator.h:34:0,
                 from /usr/include/c++/4.6/bits/allocator.h:48,
                 from /usr/include/c++/4.6/string:43,
                 from /usr/include/c++/4.6/bits/locale_classes.h:42,
                 from /usr/include/c++/4.6/bits/ios_base.h:43,
                 from /usr/include/c++/4.6/ios:43,
                 from /usr/include/c++/4.6/ostream:40,
                 from /usr/include/c++/4.6/iostream:40,
                 from usingleda.cpp:1:
/usr/include/c++/4.6/ext/new_allocator.h: In member function ‘void __gnu_cxx::new_allocator<_Tp>::construct(__gnu_cxx::new_allocator<_Tp>::pointer, _Args&& ...) [with _Args = {std::unique_ptr<lemon::ListDigraph>&}, _Tp = std::unique_ptr<lemon::ListDigraph>, __gnu_cxx::new_allocator<_Tp>::pointer = std::unique_ptr<lemon::ListDigraph>*]’:
/usr/include/c++/4.6/bits/vector.tcc:97:6:   instantiated from ‘void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {std::unique_ptr<lemon::ListDigraph>&}, _Tp = std::unique_ptr<lemon::ListDigraph>, _Alloc = std::allocator<std::unique_ptr<lemon::ListDigraph> >]’
usingleda.cpp:134:43:   instantiated from here
/usr/include/c++/4.6/ext/new_allocator.h:114:4: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = lemon::ListDigraph, _Dp = std::default_delete<lemon::ListDigraph>, std::unique_ptr<_Tp, _Dp> = std::unique_ptr<lemon::ListDigraph>]’
/usr/include/c++/4.6/bits/unique_ptr.h:256:7: error: declared here
In file included from /usr/include/c++/4.6/vector:70:0,
                 from /usr/local/include/lemon/core.h:22,
                 from /usr/local/include/lemon/list_graph.h:26,
                 from usingleda.cpp:3:
/usr/include/c++/4.6/bits/vector.tcc: In member function ‘void std::vector<_Tp, _Alloc>::_M_insert_aux(std::vector<_Tp, _Alloc>::iterator, _Args&& ...) [with _Args = {std::unique_ptr<lemon::ListDigraph>&}, _Tp = std::unique_ptr<lemon::ListDigraph>, _Alloc = std::allocator<std::unique_ptr<lemon::ListDigraph> >, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<std::unique_ptr<lemon::ListDigraph>*, std::vector<std::unique_ptr<lemon::ListDigraph> > >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = std::unique_ptr<lemon::ListDigraph>*]’:
/usr/include/c++/4.6/bits/vector.tcc:102:4:   instantiated from ‘void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {std::unique_ptr<lemon::ListDigraph>&}, _Tp = std::unique_ptr<lemon::ListDigraph>, _Alloc = std::allocator<std::unique_ptr<lemon::ListDigraph> >]’
usingleda.cpp:134:43:   instantiated from here
/usr/include/c++/4.6/bits/vector.tcc:319:4: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = lemon::ListDigraph, _Dp = std::default_delete<lemon::ListDigraph>, std::unique_ptr<_Tp, _Dp> = std::unique_ptr<lemon::ListDigraph>]’
/usr/include/c++/4.6/bits/unique_ptr.h:256:7: error: declared here
rici
  • 234,347
  • 28
  • 237
  • 341
vigenere
  • 197
  • 3
  • 15

1 Answers1

2

You are not permitted to make copies of this object (This is what the author wants). The author of the library made the copy constructor private to ensure that you can't do this.

The solution is to change std::vector<Graph> to std::vector<std::unique_ptr<Graph>>

That way, you don't need to make copies of the object as the author intended.

You would then allocate and insert like this:

std::unique_ptr<Graph> graph(new Graph);
// ...
vec.emplace_back(graph);
user123
  • 8,970
  • 2
  • 31
  • 52
  • 1
    You should certainly NOT push a unique_ptr of a stack allocated object by taking it's address. You should use `new` to allocate it. – Dave S Jun 18 '13 at 14:18
  • I'm giving him a general solution. Whether he chooses to allocate on the stack or the heap is his choice, and he should definitely be allocating on the heap. – user123 Jun 18 '13 at 14:19
  • I have updated the answer to take note of this so he can avoid the error. Thank you for pointing it out Dave – user123 Jun 18 '13 at 14:25
  • grt..thnks...solvd the prblm...can you plz jst tell me one more thing.. now i have this... vector > names; and another vector - vector state .... now i want to find out names[i][state[i]] ... that is... to which name state[i] maps to using map names[i] ... how do i do this.. – vigenere Jun 18 '13 at 14:28
  • 1
    It's best to create a new question for that. – user123 Jun 18 '13 at 14:43
  • If i use unique_ptr as you mentioned, then i am unable to use the functions of the Graph class. I am getting errors : "error: ‘Translator::up’ has no member named ‘addNode’" where "up" is "std::unique_ptr" – vigenere Jun 18 '13 at 16:28
  • 1
    `up->addNode` instead of `up.addNode` – user123 Jun 18 '13 at 16:29
  • I have edited my question to include the error i am getting after your suggested modifications. – vigenere Jun 18 '13 at 16:53
  • 1
    You have to understand that `std::unique_ptr` is non-copyable but moveable and it can be moved around with `std::move`. Until you know how to use it properly on your own, you can get this all to compile by using `std::shared_ptr` which is copyable. (You would then call `push_back` instead of `emplace_back`) – user123 Jun 18 '13 at 19:38