c++ map creation and allocation in a class member -


i transferring language c++, complete beginner c++ , trying learn essentials around struct creation , use in maps. have sketched below:

struct cubic {     int x, y, z;     cubic neighbor(int); };  struct space{     cubic location;      bool open;          bool blocked; };  class table {     int bound;     cubic center;     std::map<cubic, space> grid;     table* new(int, cubic); };  std::map<cubic, space> allocate(int bound, cubic center) {     std::map<cubic, space> ret;     (int x = -bound; x <= bound; x++) {         (int y = std::max(-bound, -x-bound); y <= std::min(bound, -x+bound); y++) {             int z = -x - y;             cubic cb = cubicadd(center, {x, y, z});             ret[cb] = {location: cb, open: true};        //this huge error, see below         };     };     return ret; } 

it table cubic coordinates hexagons. other code can posted, not relevant matter @ hand; i.e. works in there , have no problems understanding that, issue in trying understanding of basic c++ coding, here map of cubic structs keys space pointers embedded in class table....which have not gotten instantiating yet.

error:

doing

c++ table.cc -std=c++0x (for 2 files have table.h & table.cc) generates this:

in file included /usr/include/c++/4.8/bits/stl_tree.h:63:0, /usr/include/c++/4.8/map:60, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_function.h: in instantiation of ‘bool std::less<_tp>::operator()(const _tp&, const _tp&) const [with _tp = cubic]’: /usr/include/c++/4.8/bits/stl_map.h:463:31:   required ‘std::map<_key, _tp, _compare, _alloc>::mapped_type& std::map<_key, _tp, _compare, _alloc>::operator[](const key_type&) [with _key = cubic; _tp = space; _compare = std::less<cubic>; _alloc = std::allocator<std::pair<const cubic, space> >; std::map<_key, _tp, _compare, _alloc>::mapped_type = space; std::map<_key, _tp, _compare, _alloc>::key_type = cubic]’ table.cc:63:13:   required here /usr/include/c++/4.8/bits/stl_function.h:235:20: error: no match ‘operator<’ (operand types ‘const cubic’ , ‘const cubic’) { return __x < __y; } ^ /usr/include/c++/4.8/bits/stl_function.h:235:20: note: candidates are: in file included /usr/include/c++/4.8/bits/stl_algobase.h:64:0, /usr/include/c++/4.8/bits/stl_tree.h:61, /usr/include/c++/4.8/map:60, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_pair.h:220:5: note: template<class _t1, class _t2> constexpr bool std::operator<(const std::pair<_t1, _t2>&, const std::pair<_t1, _t2>&) operator<(const pair<_t1, _t2>& __x, const pair<_t1, _t2>& __y) ^ /usr/include/c++/4.8/bits/stl_pair.h:220:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.8/bits/stl_tree.h:63:0, /usr/include/c++/4.8/map:60, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const cubic’ not derived ‘const std::pair<_t1, _t2>’ { return __x < __y; } ^ in file included /usr/include/c++/4.8/bits/stl_algobase.h:67:0, /usr/include/c++/4.8/bits/stl_tree.h:61, /usr/include/c++/4.8/map:60, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_iterator.h:297:5: note: template<class _iterator> bool std::operator<(const std::reverse_iterator<_iterator>&, const std::reverse_iterator<_iterator>&) operator<(const reverse_iterator<_iterator>& __x, ^ /usr/include/c++/4.8/bits/stl_iterator.h:297:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.8/bits/stl_tree.h:63:0, /usr/include/c++/4.8/map:60, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const cubic’ not derived ‘const std::reverse_iterator<_iterator>’ { return __x < __y; } ^ in file included /usr/include/c++/4.8/bits/stl_algobase.h:67:0, /usr/include/c++/4.8/bits/stl_tree.h:61, /usr/include/c++/4.8/map:60, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_iterator.h:347:5: note: template<class _iteratorl, class _iteratorr> bool std::operator<(const std::reverse_iterator<_iterator>&, const std::reverse_iterator<_iteratorr>&) operator<(const reverse_iterator<_iteratorl>& __x, ^ /usr/include/c++/4.8/bits/stl_iterator.h:347:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.8/bits/stl_tree.h:63:0, /usr/include/c++/4.8/map:60, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const cubic’ not derived ‘const std::reverse_iterator<_iterator>’ { return __x < __y; } ^ in file included /usr/include/c++/4.8/bits/stl_algobase.h:67:0, /usr/include/c++/4.8/bits/stl_tree.h:61, /usr/include/c++/4.8/map:60, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_iterator.h:1055:5: note: template<class _iteratorl, class _iteratorr> bool std::operator<(const std::move_iterator<_iterator>&, const std::move_iterator<_iteratorr>&) operator<(const move_iterator<_iteratorl>& __x, ^ /usr/include/c++/4.8/bits/stl_iterator.h:1055:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.8/bits/stl_tree.h:63:0, /usr/include/c++/4.8/map:60, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const cubic’ not derived ‘const std::move_iterator<_iterator>’ { return __x < __y; } ^ in file included /usr/include/c++/4.8/bits/stl_algobase.h:67:0, /usr/include/c++/4.8/bits/stl_tree.h:61, /usr/include/c++/4.8/map:60, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_iterator.h:1061:5: note: template<class _iterator> bool std::operator<(const std::move_iterator<_iterator>&, const std::move_iterator<_iterator>&) operator<(const move_iterator<_iterator>& __x, ^ /usr/include/c++/4.8/bits/stl_iterator.h:1061:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.8/bits/stl_tree.h:63:0, /usr/include/c++/4.8/map:60, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const cubic’ not derived ‘const std::move_iterator<_iterator>’ { return __x < __y; } ^ in file included /usr/include/c++/4.8/map:60:0, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_tree.h:917:5: note: template<class _key, class _val, class _keyofvalue, class _compare, class _alloc> bool std::operator<(const std::_rb_tree<_key, _val, _keyofvalue, _compare, _alloc>&, const std::_rb_tree<_key, _val, _keyofvalue, _compare, _alloc>&) operator<(const _rb_tree<_key, _val, _keyofvalue, _compare, _alloc>& __x, ^ /usr/include/c++/4.8/bits/stl_tree.h:917:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.8/bits/stl_tree.h:63:0, /usr/include/c++/4.8/map:60, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const cubic’ not derived ‘const std::_rb_tree<_key, _val, _keyofvalue, _compare, _alloc>’ { return __x < __y; } ^ in file included /usr/include/c++/4.8/string:52:0, /usr/include/c++/4.8/stdexcept:39, /usr/include/c++/4.8/array:38, /usr/include/c++/4.8/tuple:39, /usr/include/c++/4.8/bits/stl_map.h:63, /usr/include/c++/4.8/map:61, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/basic_string.h:2569:5: note: template<class _chart, class _traits, class _alloc> bool std::operator<(const std::basic_string<_chart, _traits, _alloc>&, const std::basic_string<_chart, _traits, _alloc>&) operator<(const basic_string<_chart, _traits, _alloc>& __lhs, ^ /usr/include/c++/4.8/bits/basic_string.h:2569:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.8/bits/stl_tree.h:63:0, /usr/include/c++/4.8/map:60, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const cubic’ not derived ‘const std::basic_string<_chart, _traits, _alloc>’ { return __x < __y; } ^ in file included /usr/include/c++/4.8/string:52:0, /usr/include/c++/4.8/stdexcept:39, /usr/include/c++/4.8/array:38, /usr/include/c++/4.8/tuple:39, /usr/include/c++/4.8/bits/stl_map.h:63, /usr/include/c++/4.8/map:61, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/basic_string.h:2581:5: note: template<class _chart, class _traits, class _alloc> bool std::operator<(const std::basic_string<_chart, _traits, _alloc>&, const _chart*) operator<(const basic_string<_chart, _traits, _alloc>& __lhs, ^ /usr/include/c++/4.8/bits/basic_string.h:2581:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.8/bits/stl_tree.h:63:0, /usr/include/c++/4.8/map:60, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const cubic’ not derived ‘const std::basic_string<_chart, _traits, _alloc>’ { return __x < __y; } ^ in file included /usr/include/c++/4.8/string:52:0, /usr/include/c++/4.8/stdexcept:39, /usr/include/c++/4.8/array:38, /usr/include/c++/4.8/tuple:39, /usr/include/c++/4.8/bits/stl_map.h:63, /usr/include/c++/4.8/map:61, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/basic_string.h:2593:5: note: template<class _chart, class _traits, class _alloc> bool std::operator<(const _chart*, const std::basic_string<_chart, _traits, _alloc>&) operator<(const _chart* __lhs, ^ /usr/include/c++/4.8/bits/basic_string.h:2593:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.8/bits/stl_tree.h:63:0, /usr/include/c++/4.8/map:60, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_function.h:235:20: note:   mismatched types ‘const _chart*’ , ‘cubic’ { return __x < __y; } ^ in file included /usr/include/c++/4.8/tuple:39:0, /usr/include/c++/4.8/bits/stl_map.h:63, /usr/include/c++/4.8/map:61, table.h:3, table.cc:1: /usr/include/c++/4.8/array:238:5: note: template<class _tp, long unsigned int _nm> bool std::operator<(const std::array<_tp, _nm>&, const std::array<_tp, _nm>&) operator<(const array<_tp, _nm>& __a, const array<_tp, _nm>& __b) ^ /usr/include/c++/4.8/array:238:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.8/bits/stl_tree.h:63:0, /usr/include/c++/4.8/map:60, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const cubic’ not derived ‘const std::array<_tp, _nm>’ { return __x < __y; } ^ in file included /usr/include/c++/4.8/bits/stl_map.h:63:0, /usr/include/c++/4.8/map:61, table.h:3, table.cc:1: /usr/include/c++/4.8/tuple:824:5: note: template<class ... _telements, class ... _uelements> constexpr bool std::operator<(const std::tuple<_args1 ...>&, const std::tuple<_args2 ...>&) operator<(const tuple<_telements...>& __t, ^ /usr/include/c++/4.8/tuple:824:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.8/bits/stl_tree.h:63:0, /usr/include/c++/4.8/map:60, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const cubic’ not derived ‘const std::tuple<_args1 ...>’ { return __x < __y; } ^ in file included /usr/include/c++/4.8/map:61:0, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_map.h:979:5: note: template<class _key, class _tp, class _compare, class _alloc> bool std::operator<(const std::map<_key, _tp, _compare, _alloc>&, const std::map<_key, _tp, _compare, _alloc>&) operator<(const map<_key, _tp, _compare, _alloc>& __x, ^ /usr/include/c++/4.8/bits/stl_map.h:979:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.8/bits/stl_tree.h:63:0, /usr/include/c++/4.8/map:60, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const cubic’ not derived ‘const std::map<_key, _tp, _compare, _alloc>’ { return __x < __y; } ^ in file included /usr/include/c++/4.8/map:62:0, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_multimap.h:881:5: note: template<class _key, class _tp, class _compare, class _alloc> bool std::operator<(const std::multimap<_key, _tp, _compare, _alloc>&, const std::multimap<_key, _tp, _compare, _alloc>&) operator<(const multimap<_key, _tp, _compare, _alloc>& __x, ^ /usr/include/c++/4.8/bits/stl_multimap.h:881:5: note:   template argument deduction/substitution failed: in file included /usr/include/c++/4.8/bits/stl_tree.h:63:0, /usr/include/c++/4.8/map:60, table.h:3, table.cc:1: /usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const cubic’ not derived ‘const std::multimap<_key, _tp, _compare, _alloc>’ { return __x < __y; } ^ 

you didn't post definition of cubic, issue first error:

/usr/include/c++/4.8/bits/stl_function.h: in instantiation of ‘bool std::less<_tp>::operator()(const _tp&, const _tp&) const [with _tp = cubic]’: /usr/include/c++/4.8/bits/stl_map.h:463:31:   required ‘std::map<_key, _tp, _compare, _alloc>::mapped_type& std::map<_key, _tp, _compare, _alloc>::operator[](const key_type&) [with _key = cubic; _tp = space; _compare = std::less<cubic>; _alloc = std::allocator<std::pair<const cubic, space> >; std::map<_key, _tp, _compare, _alloc>::mapped_type = space; std::map<_key, _tp, _compare, _alloc>::key_type = cubic]’ table.cc:63:13:   required here /usr/include/c++/4.8/bits/stl_function.h:235:20: error: no match ‘operator<’ (operand types ‘const cubic’ , ‘const cubic’) { return __x < __y; } ^ 

std::map binary tree (red-black tree specifically) , needs keys lessthancomparable. default, uses operator< , key (cubic) not have member or non-member operator< defined. rest of compile error compiler trying stick cubic other operator<'s can find other type - none of apply since cubic isn't convertible of those.

so either provide one:

bool operator<(const cubic& a, const cubic& b) {     // reasonable,     return std::tie(a.x, a.y, a.z) < std::tie(b.x, b.y, b.z); } 

or provide map custom comparator:

struct cubiccompare {     bool operator()(const cubic& a, const cubic& b) const {         // reasonable, return true if "less than" b     } };  std::map<cubic, space, cubiccompare> ret; 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -