2012-05-10 11 views
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); 

これは私が

In member function 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Point]':|

instantiated from '_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = Point, _Tp = std::vector, std::allocator >, std::allocator, std::allocator > > >, _Compare = std::less, _Alloc = std::allocator, std::allocator >, std::allocator, |

instantiated from here|

c:\program files (no match for 'operator<' in '__x < __y'| ||=== Build finished: 1 errors, 0 warnings ===|

答えて

8

あなたのエラーがstd::vector<>std::map<>とは全く無関係である取得エラーであるが、その鍵はoperator<と同等であることのどちらかということ、またはカスタムコンパレータを供給する必要があります。私のお気に入りのオンラインリファレンスit readsチェック

bool operator <(Point const& lhs, Point const& rhs) 
{ 
    return lhs.y < rhs.y || lhs.y == rhs.y && lhs.x < rhs.x; 
} 
15

:最も簡単な解決策はPointの定義の後に以下を追加することです

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

Map is an associative container that contains a sorted list of unique key-value pairs. That list is sorted using the comparison function Compare applied to the keys. Search, removal, and insertion operations have logarithmic complexity. Maps are usually implemented as red-black trees.

あなたはそれがデフォルトstd::less<Key>を使用してソートし、明示的なCompareを提供していないので、 。エラーは、そのクラスにあるので、我々は正しい軌道に乗っているように思える:

In member function 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Point]':|

レッツ・check that

template< class T > 
struct less; 

Function object for performing comparisons. Uses operator< on type T .

これは、エラーメッセージが私達に告げるものと一致:

no match for 'operator<' in '__x < __y'

ええ、Pointの場合はoperator<がありません...

+2

ギビング答え:良い。エラーを説明する:貴重な。 – chris

関連する問題