質問

私は、テスト何かに実行する小さなプログラムを持っている。

#include <map>
#include <iostream>
using namespace std;

struct _pos{
        float xi;
        float xf;

        bool operator<(_pos& other){

                return this->xi < other.xi;
        }
};

struct _val{

        float f;
};

int main()
{
        map<_pos,_val> m;

        struct  _pos k1 = {0,10};
        struct  _pos k2 = {10,15};

        struct  _val v1 = {5.5};
        struct  _val v2 = {12.3};                                                                   

        m.insert(std::pair<_pos,_val>(k1,v1));
        m.insert(std::pair<_pos,_val>(k2,v2));

        return 0;
}

問題は、私はそれをコンパイルしようとすると、私は

次のエラーを取得するということです
$ g++ m2.cpp -o mtest
In file included from /usr/include/c++/4.4/bits/stl_tree.h:64,
                 from /usr/include/c++/4.4/map:60,
                 from m2.cpp:1:
/usr/include/c++/4.4/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = _pos]’:
/usr/include/c++/4.4/bits/stl_tree.h:1170:   instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = _pos, _Val = std::pair<const _pos, _val>, _KeyOfValue = std::_Select1st<std::pair<const _pos, _val> >, _Compare = std::less<_pos>, _Alloc = std::allocator<std::pair<const _pos, _val> >]’
/usr/include/c++/4.4/bits/stl_map.h:500:   instantiated from ‘std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const std::pair<const _Key, _Tp>&) [with _Key = _pos, _Tp = _val, _Compare = std::less<_pos>, _Alloc = std::allocator<std::pair<const _pos, _val> >]’
m2.cpp:30:   instantiated from here
/usr/include/c++/4.4/bits/stl_function.h:230: error: no match for ‘operator<’ in ‘__x < __y’
m2.cpp:9: note: candidates are: bool _pos::operator<(_pos&)
$ 

私は、キーの上に<演算子を宣言しても問題が解決したが、そのまだそこだろうと思っています。

間違っている可能性は?

事前に感謝します。

役に立ちましたか?

解決

問題はこれです:

bool operator<(_pos& other)

このある必要があります:

bool operator<(const _pos& other) const {
//             ^^^^               ^^^^^

constなし関数は、引数を変更することができるので、第一bなければ、比較(a < bconst)の右辺は、constことができない。

constことなく機能がaを変更することができるので、第a < bなければ、比較(constconst)の左辺は、thisことができない。

内部的には、マップのキーのは常にconstされます。

<時間>

あなたが非会員機能を使用することを好むことに留意すべきです。それは、より良いフリー機能は、次のとおりです。

bool operator<(const _pos& lhs, const _pos& rhs)
{
    return lhs.xi < rhs.xi;
}
あなたのクラスと同じ名前空間には、

。 (この例では、ちょうどその下に。)

<時間> ちなみに、C ++でstructを持つ構造体の型の変数の宣言を先頭に付加する必要はありません。これは完璧であり、好ましいます:

    _pos k1 = {0,10};
    _pos k2 = {10,15};

    _val v1 = {5.5};
    _val v2 = {12.3};

(あなたのタイプ名は明らかに非正統的な方法で命名されているが:P)

<時間>

最後に、あなたがペアを作るためmake_pairユーティリティ機能を好む必要があります:

    m.insert(std::make_pair(k1,v1));
    m.insert(std::make_pair(k2,v2));

これは、ペアの種類を記述することからあなたを保存し、一般的に読みやすいです。 (長いタイプ名が一緒に来る場合は特に。)

他のヒント

このメンバ関数が非constとして宣言されているので、

以下、オペレータがbool operator<(const _pos& other) const必要以上の署名、そうでなければマップはCONST関数でこの演算子を使用することはできません。

右手側(この場合は引数が)constとしてマークする必要があり、それは例えば、constメンバ関数であるべき

-

私は、オペレータのあなたの定義<が間違っていると思います

    bool operator<(const _pos& other) const{ 

            return this->xi < other.xi; 
    } 
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top