c++11 - C++ unordered_map move beginner error -


sorry noob question here, don't know search on google. tried documention on move semantic still can't solve problem.

so if can give me clues, i'll thanksfull.

about 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 want add new element in unordered_map move. it's 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 src/test.cpp:1: in file included ./src/include/graph.hpp:15: in file included /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/unordered_map:41: in file included /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/tuple:39: in file included /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/array:38: in file included /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/stdexcept:39: in file included /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/string:40: in file included /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 assigned because copy       assignment operator 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> >' implicitly deleted because field '_endnode' of reference type       'node<std::basic_string<char>, std::basic_string<char> > &'         node<t, u>&  _endnode;                      ^ in file included src/test.cpp:1: in file included ./src/include/graph.hpp:15: in file included /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/unordered_map:41: in file included /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/tuple:39: in file included /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/array:38: in file included /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/stdexcept:39: in file included /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/string:40: in file included /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 assigned because copy       assignment operator 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> >' implicitly deleted because field '_endnode' of reference type       'node<std::basic_string<char>, std::basic_string<char> > &'         node<t, u>&  _endnode;                      ^ 2 errors generated. makefile:43: recipe target 'obj/test.o' failed make: *** [obj/test.o] error 1 

sorry trouble, have nice day.

okay. problem in line:

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

you trying move-assign node<t, u>. however, node not have move assignment operator implicitly declared defaulted because there user-declared destructor:

~node() = default; 

as result, line calls copy assignment operator of node<t, u>, performs member-wise copy. 1 of members of node<t, u> of type std::vector<link<t, u>>, link<t, u> has member variable reference. references aren't copy-assignable, copy assignment operator implicitly deleted. hence, there no copy assignment operator found, line ill-formed.

while references aren't move-assignable either, vector<link<t,u>> move-assignable, simplest solution delete line declare destructor defaulted:

//~node() = default; 

with that, compiler implicitly declare default move constructor/assignment, , code expect. , compile.

side-note, bad idea:

public:     #include  "graph.tpp" }; 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -