C++ unordered_map move beginner error

276 views Asked by At

Sorry for the noob question here, but i don't even know what to search on google. I tried some documention on the move semantic but i still can't solve my problem.

So if somebody can give me some clues, i'll be really thanksfull.

About the code:

graph.hpp:

#ifndef  GRAPH_HPP
#define  GRAPH_HPP

#include  <cstdlib>
#include  <unordered_map>
#include  <vector>
#include  <string>

template<typename T, typename U>
class  Link;
template<typename T, typename U>
class  Node;

namespace  graph
{
  template<typename T, typename U>
  class  Link
    {
      public:
    Link() = default;
    Link(U&& data, T& node) : _data(data), _endNode(node) {}
    ~Link() = default;

      private:
    U            _data;
    Node<T, U>&  _endNode;
    };

  template<typename T, typename U>
  class  Node
    {
      public:
    Node() = default;
    Node(T&& data) : _data(data) {};
    ~Node() = default;

    private:
      T                        _data;
      std::vector<Link<T, U>>  _links;
    };

  template<typename K, typename T, typename U>
  class  Graph
    {
      public:
        Graph() = default;
        ~Graph() = default;

      private:
        size_t                             _nbNode;
        std::unordered_map<K, Node<T, U>>  _nodes;

      public:
        #include  "graph.tpp"
    };
}
#endif

graph.tpp:

void  add_Link(const K& key1,
           const K& key2,
           U&& linkInfo)
{
  if (!_nodes.count(key1)
      || !_nodes.count(key2))
    throw "Key doesn't exist!";

  Link<T, U>  newLink(linkInfo, _nodes[key2]);
  _nodes[key1].links.push_back(newLink);
}

void  add_Node(K& key, T&& elem)
{
  Node<T, U>  newNode(std::move(elem));
  _nodes[key] = std::move(newNode);
}

main.cpp:

#include  "graph.hpp"
#include  <string>

int main()
{
  std::string  name("name");
  graph::Graph<std::string, std::string, std::string>  graph;

  graph.add_Node(name, "lol");
  return 0;
}

I just want to add a new element in the unordered_map with a move. But it's just say me :

    clang++ -Wall -Wextra -std=c++14 -O2 -march=native -s -fomit-frame-pointer -c src/test.cpp -o obj/test.o -I ./src/include -I ./src/template/
clang: warning: argument unused during compilation: '-s'
In file included from src/test.cpp:1:
In file included from ./src/include/graph.hpp:15:
In file included from /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/unordered_map:41:
In file included from /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/tuple:39:
In file included from /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/array:38:
In file included from /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/stdexcept:39:
In file included from /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/string:40:
In file included from /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/char_traits.h:39:
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/stl_algobase.h:336:18: error: object of type 'graph::Link<std::basic_string<char>, std::basic_string<char> >' cannot be assigned because its copy
      assignment operator is implicitly deleted
              *__result = *__first;
                        ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/stl_algobase.h:396:36: note: in instantiation of function template specialization 'std::__copy_move<false, false,
      std::random_access_iterator_tag>::__copy_m<const graph::Link<std::basic_string<char>, std::basic_string<char> > *, graph::Link<std::basic_string<char>, std::basic_string<char> > *>' requested here
                              _Category>::__copy_m(__first, __last, __result);
                                          ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/stl_algobase.h:432:23: note: in instantiation of function template specialization 'std::__copy_move_a<false, const graph::Link<std::basic_string<char>,
      std::basic_string<char> > *, graph::Link<std::basic_string<char>, std::basic_string<char> > *>' requested here
      return _OI(std::__copy_move_a<_IsMove>(std::__niter_base(__first),
                      ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/stl_algobase.h:464:20: note: in instantiation of function template specialization 'std::__copy_move_a2<false, __gnu_cxx::__normal_iterator<const
      graph::Link<std::basic_string<char>, std::basic_string<char> > *, std::vector<graph::Link<std::basic_string<char>, std::basic_string<char> >, std::allocator<graph::Link<std::basic_string<char>, std::basic_string<char> > > > >,
      __gnu_cxx::__normal_iterator<graph::Link<std::basic_string<char>, std::basic_string<char> > *, std::vector<graph::Link<std::basic_string<char>, std::basic_string<char> >, std::allocator<graph::Link<std::basic_string<char>,
      std::basic_string<char> > > > > >' requested here
      return (std::__copy_move_a2<__is_move_iterator<_II>::__value>
                   ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/vector.tcc:206:27: note: in instantiation of function template specialization 'std::copy<__gnu_cxx::__normal_iterator<const
      graph::Link<std::basic_string<char>, std::basic_string<char> > *, std::vector<graph::Link<std::basic_string<char>, std::basic_string<char> >, std::allocator<graph::Link<std::basic_string<char>, std::basic_string<char> > > > >,
      __gnu_cxx::__normal_iterator<graph::Link<std::basic_string<char>, std::basic_string<char> > *, std::vector<graph::Link<std::basic_string<char>, std::basic_string<char> >, std::allocator<graph::Link<std::basic_string<char>,
      std::basic_string<char> > > > > >' requested here
              std::_Destroy(std::copy(__x.begin(), __x.end(), begin()),
                                 ^
./src/include/graph.hpp:40:10: note: in instantiation of member function 'std::vector<graph::Link<std::basic_string<char>, std::basic_string<char> >, std::allocator<graph::Link<std::basic_string<char>, std::basic_string<char> > >
      >::operator=' requested here
  class  Node
         ^
src/test.cpp:9:9: note: in instantiation of member function 'graph::Graph<std::basic_string<char>, std::basic_string<char>, std::basic_string<char> >::add_Node' requested here
  graph.add_Node(name, "lol");
        ^
./src/include/graph.hpp:36:15: note: copy assignment operator of 'Link<std::basic_string<char>, std::basic_string<char> >' is implicitly deleted because field '_endNode' is of reference type
      'Node<std::basic_string<char>, std::basic_string<char> > &'
        Node<T, U>&  _endNode;
                     ^
In file included from src/test.cpp:1:
In file included from ./src/include/graph.hpp:15:
In file included from /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/unordered_map:41:
In file included from /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/tuple:39:
In file included from /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/array:38:
In file included from /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/stdexcept:39:
In file included from /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/string:40:
In file included from /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/char_traits.h:39:
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/stl_algobase.h:336:18: error: object of type 'graph::Link<std::basic_string<char>, std::basic_string<char> >' cannot be assigned because its copy
      assignment operator is implicitly deleted
              *__result = *__first;
                        ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/stl_algobase.h:396:36: note: in instantiation of function template specialization 'std::__copy_move<false, false,
      std::random_access_iterator_tag>::__copy_m<graph::Link<std::basic_string<char>, std::basic_string<char> > *, graph::Link<std::basic_string<char>, std::basic_string<char> > *>' requested here
                              _Category>::__copy_m(__first, __last, __result);
                                          ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/stl_algobase.h:432:23: note: in instantiation of function template specialization 'std::__copy_move_a<false, graph::Link<std::basic_string<char>,
      std::basic_string<char> > *, graph::Link<std::basic_string<char>, std::basic_string<char> > *>' requested here
      return _OI(std::__copy_move_a<_IsMove>(std::__niter_base(__first),
                      ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/stl_algobase.h:464:20: note: in instantiation of function template specialization 'std::__copy_move_a2<false, graph::Link<std::basic_string<char>,
      std::basic_string<char> > *, graph::Link<std::basic_string<char>, std::basic_string<char> > *>' requested here
      return (std::__copy_move_a2<__is_move_iterator<_II>::__value>
                   ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/vector.tcc:211:13: note: in instantiation of function template specialization 'std::copy<graph::Link<std::basic_string<char>, std::basic_string<char> >
      *, graph::Link<std::basic_string<char>, std::basic_string<char> > *>' requested here
              std::copy(__x._M_impl._M_start, __x._M_impl._M_start + size(),
                   ^
./src/include/graph.hpp:40:10: note: in instantiation of member function 'std::vector<graph::Link<std::basic_string<char>, std::basic_string<char> >, std::allocator<graph::Link<std::basic_string<char>, std::basic_string<char> > >
      >::operator=' requested here
  class  Node
         ^
src/test.cpp:9:9: note: in instantiation of member function 'graph::Graph<std::basic_string<char>, std::basic_string<char>, std::basic_string<char> >::add_Node' requested here
  graph.add_Node(name, "lol");
        ^
./src/include/graph.hpp:36:15: note: copy assignment operator of 'Link<std::basic_string<char>, std::basic_string<char> >' is implicitly deleted because field '_endNode' is of reference type
      'Node<std::basic_string<char>, std::basic_string<char> > &'
        Node<T, U>&  _endNode;
                     ^
2 errors generated.
Makefile:43: recipe for target 'obj/test.o' failed
make: *** [obj/test.o] Error 1

Sorry for the trouble, have a nice day.

1

There are 1 answers

7
Barry On BEST ANSWER

Okay. The problem is in this line:

_nodes[key] = std::move(newNode);

You are trying to move-assign Node<T, U>. However, Node will not have a move assignment operator implicitly declared as defaulted because there is a user-declared destructor:

~Node() = default;

As a result, that line actually calls the copy assignment operator of Node<T, U>, which performs a member-wise copy. One of the members of Node<T, U> is of type std::vector<Link<T, U>>, where Link<T, U> has a member variable that is a reference. References aren't copy-assignable, so the copy assignment operator is implicitly deleted. Hence, there is no copy assignment operator found, so the line is ill-formed.

While references aren't move-assignable either, a vector<Link<T,U>> is move-assignable, so the simplest solution is to just delete the line where you declare the destructor as defaulted:

//~Node() = default;

With that, the compiler will implicitly declare the default move constructor/assignment, and this code will do what you expect. And compile.

Side-note, this is a really bad idea:

public:
    #include  "graph.tpp"
};