在类成员中创建和分配映射

c++ map creation and allocation in a class member

本文关键字:分配 映射 创建 成员      更新时间:2023-10-16

我正在从另一种语言转移到c++,作为一个完整的c++初学者,我正在努力学习一些围绕结构创建和在地图中使用的基本要素。我在下面画了个草图:

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;
    for (int x = -bound; x <= bound; x++) {
        for (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 IS A HUGE ERROR, see below
        };
    };
    return ret;
}

它基本上是六边形的表立方坐标。其他代码可以张贴,但它与手头的事情无关;即,它在那里工作,我没有问题的理解,问题是在试图得到基本的c++编码的理解,这里是立方结构键空间指针嵌入在类表....的地图我甚至还没有实例化它

错误:

c++表。cc -std=c++0x(对于2个文件,我有table.h &Table.cc)生成如下:

In file included from /usr/include/c++/4.8/bits/stl_tree.h:63:0,
from /usr/include/c++/4.8/map:60,
from table.h:3,
from 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 from ‘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 from here
/usr/include/c++/4.8/bits/stl_function.h:235:20: error: no match for ‘operator<’ (operand types are ‘const Cubic’ and ‘const Cubic’)
{ return __x < __y; }
^
/usr/include/c++/4.8/bits/stl_function.h:235:20: note: candidates are:
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:64:0,
from /usr/include/c++/4.8/bits/stl_tree.h:61,
from /usr/include/c++/4.8/map:60,
from table.h:3,
from 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 from /usr/include/c++/4.8/bits/stl_tree.h:63:0,
from /usr/include/c++/4.8/map:60,
from table.h:3,
from table.cc:1:
/usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const Cubic’ is not derived from ‘const std::pair<_T1, _T2>’
{ return __x < __y; }
^
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:67:0,
from /usr/include/c++/4.8/bits/stl_tree.h:61,
from /usr/include/c++/4.8/map:60,
from table.h:3,
from 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 from /usr/include/c++/4.8/bits/stl_tree.h:63:0,
from /usr/include/c++/4.8/map:60,
from table.h:3,
from table.cc:1:
/usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const Cubic’ is not derived from ‘const std::reverse_iterator<_Iterator>’
{ return __x < __y; }
^
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:67:0,
from /usr/include/c++/4.8/bits/stl_tree.h:61,
from /usr/include/c++/4.8/map:60,
from table.h:3,
from 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 from /usr/include/c++/4.8/bits/stl_tree.h:63:0,
from /usr/include/c++/4.8/map:60,
from table.h:3,
from table.cc:1:
/usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const Cubic’ is not derived from ‘const std::reverse_iterator<_Iterator>’
{ return __x < __y; }
^
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:67:0,
from /usr/include/c++/4.8/bits/stl_tree.h:61,
from /usr/include/c++/4.8/map:60,
from table.h:3,
from 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 from /usr/include/c++/4.8/bits/stl_tree.h:63:0,
from /usr/include/c++/4.8/map:60,
from table.h:3,
from table.cc:1:
/usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const Cubic’ is not derived from ‘const std::move_iterator<_Iterator>’
{ return __x < __y; }
^
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:67:0,
from /usr/include/c++/4.8/bits/stl_tree.h:61,
from /usr/include/c++/4.8/map:60,
from table.h:3,
from 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 from /usr/include/c++/4.8/bits/stl_tree.h:63:0,
from /usr/include/c++/4.8/map:60,
from table.h:3,
from table.cc:1:
/usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const Cubic’ is not derived from ‘const std::move_iterator<_Iterator>’
{ return __x < __y; }
^
In file included from /usr/include/c++/4.8/map:60:0,
from table.h:3,
from 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 from /usr/include/c++/4.8/bits/stl_tree.h:63:0,
from /usr/include/c++/4.8/map:60,
from table.h:3,
from table.cc:1:
/usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const Cubic’ is not derived from ‘const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>’
{ return __x < __y; }
^
In file included from /usr/include/c++/4.8/string:52:0,
from /usr/include/c++/4.8/stdexcept:39,
from /usr/include/c++/4.8/array:38,
from /usr/include/c++/4.8/tuple:39,
from /usr/include/c++/4.8/bits/stl_map.h:63,
from /usr/include/c++/4.8/map:61,
from table.h:3,
from 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 from /usr/include/c++/4.8/bits/stl_tree.h:63:0,
from /usr/include/c++/4.8/map:60,
from table.h:3,
from table.cc:1:
/usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const Cubic’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
{ return __x < __y; }
^
In file included from /usr/include/c++/4.8/string:52:0,
from /usr/include/c++/4.8/stdexcept:39,
from /usr/include/c++/4.8/array:38,
from /usr/include/c++/4.8/tuple:39,
from /usr/include/c++/4.8/bits/stl_map.h:63,
from /usr/include/c++/4.8/map:61,
from table.h:3,
from 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 from /usr/include/c++/4.8/bits/stl_tree.h:63:0,
from /usr/include/c++/4.8/map:60,
from table.h:3,
from table.cc:1:
/usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const Cubic’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
{ return __x < __y; }
^
In file included from /usr/include/c++/4.8/string:52:0,
from /usr/include/c++/4.8/stdexcept:39,
from /usr/include/c++/4.8/array:38,
from /usr/include/c++/4.8/tuple:39,
from /usr/include/c++/4.8/bits/stl_map.h:63,
from /usr/include/c++/4.8/map:61,
from table.h:3,
from 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 from /usr/include/c++/4.8/bits/stl_tree.h:63:0,
from /usr/include/c++/4.8/map:60,
from table.h:3,
from table.cc:1:
/usr/include/c++/4.8/bits/stl_function.h:235:20: note:   mismatched types ‘const _CharT*’ and ‘Cubic’
{ return __x < __y; }
^
In file included from /usr/include/c++/4.8/tuple:39:0,
from /usr/include/c++/4.8/bits/stl_map.h:63,
from /usr/include/c++/4.8/map:61,
from table.h:3,
from 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 from /usr/include/c++/4.8/bits/stl_tree.h:63:0,
from /usr/include/c++/4.8/map:60,
from table.h:3,
from table.cc:1:
/usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const Cubic’ is not derived from ‘const std::array<_Tp, _Nm>’
{ return __x < __y; }
^
In file included from /usr/include/c++/4.8/bits/stl_map.h:63:0,
from /usr/include/c++/4.8/map:61,
from table.h:3,
from 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 from /usr/include/c++/4.8/bits/stl_tree.h:63:0,
from /usr/include/c++/4.8/map:60,
from table.h:3,
from table.cc:1:
/usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const Cubic’ is not derived from ‘const std::tuple<_Args1 ...>’
{ return __x < __y; }
^
In file included from /usr/include/c++/4.8/map:61:0,
from table.h:3,
from 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 from /usr/include/c++/4.8/bits/stl_tree.h:63:0,
from /usr/include/c++/4.8/map:60,
from table.h:3,
from table.cc:1:
/usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const Cubic’ is not derived from ‘const std::map<_Key, _Tp, _Compare, _Alloc>’
{ return __x < __y; }
^
In file included from /usr/include/c++/4.8/map:62:0,
from table.h:3,
from 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 from /usr/include/c++/4.8/bits/stl_tree.h:63:0,
from /usr/include/c++/4.8/map:60,
from table.h:3,
from table.cc:1:
/usr/include/c++/4.8/bits/stl_function.h:235:20: note:   ‘const Cubic’ is not derived from ‘const std::multimap<_Key, _Tp, _Compare, _Alloc>’
{ return __x < __y; }
^

您没有发布Cubic的定义,但问题来自第一个错误:

/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 from ‘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 from here
/usr/include/c++/4.8/bits/stl_function.h:235:20: error: no match for ‘operator<’ (operand types are ‘const Cubic’ and ‘const Cubic’)
{ return __x < __y; }
^

std::map是一个二叉树(特别是红黑树),它需要它的键是LessThanComparable。默认情况下,它使用operator<,并且您的Key (Cubic)没有定义成员或非成员operator<。编译错误的其余部分是编译器试图将Cubic粘贴到它可以为任何其他类型找到的所有其他operator<中-这些都不适用,因为Cubic不能转换为其中的任何一个。

所以要么提供一个:

bool operator<(const Cubic& a, const Cubic& b) {
    // something reasonable, probably like
    return std::tie(a.X, a.Y, a.Z) < std::tie(b.X, b.Y, b.Z);
}

或者为map提供一个自定义比较器:

struct CubicCompare {
    bool operator()(const Cubic& a, const Cubic& b) const {
        // something reasonable, return true if a is "less than" b
    }
};
std::map<Cubic, Space, CubicCompare> ret;

std::map<>需要比较运算符对其数据进行排序和排序。默认情况下,它使用<操作符。查看您的错误,看起来这样的操作符没有在您的Cubic类中实现。您可以通过在Cubic类中重载<或使用自定义比较器来解决此问题。

这里是自定义比较器的信息。