0

ポイントマップにポイントのベクトルを設定しようとしています。ボード上の各位置にポイント (x、y) と正当な動きのベクトル (ポイント オブジェクト) があるボード ゲームを作成しようとしています。

マップ KEY をポイントとして持つことができないようです。

struct Point
{
    Point() {}
    Point(int ix, int iy ) :x(ix), y(iy) {}

    int x;
    int y;
};


Point p_source (2,2);
Point p_next1 (1,2);
Point p_next2 (1,3);
Point p_next3 (1,4);

map <Point, vector<Point> > m_point;

dict[p_source].push_back(p_next1);
dict[p_source].push_back(p_next2);
dict[p_source].push_back(p_next3);

これは私が得るエラーです

メンバー関数内 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Point]':|

'_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [ with _Key = Point, _Tp = std::vector, std::allocator >, std::allocator, std::アロケーター > > >、_Compare = std::less、_Alloc = std::アロケーター、std::アロケーター >、std::アロケーター、|

ここからインスタンス化|

c:\program files ('operator<' in '__x < __y'| ||=== ビルド終了: 1 エラー、0 警告 ===|

4

2 に答える 2

15

私のお気に入りのオンラインリファレンスをチェックすると、次のようになります

template<
    class Key,
    class T,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<std::pair<const Key, T> >
> class map;

マップは、一意のキーと値のペアの並べ替えられたリストを含む連想コンテナです。そのリストは Compare、キーに適用される比較機能を使用してソートされます。検索、削除、および挿入の操作には、対数的な複雑さがあります。マップは通常、赤黒木として実装されます。

明示的に指定しないためCompare、デフォルトを使用してソートしますstd::less<Key>。エラーはそのクラスにあるため、私たちは正しい方向に進んでいるようです。

メンバー関数'boolstd :: less <_Tp> :: operator()(const _Tp&、const _Tp&)const [with _Tp = Point]':|

それを確認しましょう:

template< class T >
struct less;

比較を実行するための関数オブジェクト。operator<タイプで使用しTます。

これは、エラーメッセージが示す内容と一致します。

'__x<__y'の'operator<'に一致しません

うーん、でもoperator<タイプはありませんPoint...

于 2012-05-10T18:58:58.577 に答える
8

エラーは完全に無関係ですstd::vector<>std::map<>そのキーがと同等でoperator<あるか、カスタム比較器を提供する必要があります。Point最も簡単な解決策は、の定義の後に次を追加することです。

bool operator <(Point const& lhs, Point const& rhs)
{
    return lhs.y < rhs.y || lhs.y == rhs.y && lhs.x < rhs.x;
}
于 2012-05-10T18:57:22.073 に答える