9

何かをテストするために実行したい小さなプログラムがあります

#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&)
$ 

キーでoperator<を宣言することで問題は解決すると思いましたが、それでも問題は解決しました。

何が間違っている可能性がありますか?

前もって感謝します。

4

3 に答える 3

25

問題はこれです:

bool operator<(_pos& other)

これである必要があります:

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

最初のがないと、関数がないと引数が変更される可能性があるためconst、比較の右側(bin a < b)をにすることはできません。constconst

2番目がないと、関数がないと変更される可能性があるためconst、比較の左側(ain a < b)を、にすることはできません。constconstthis

内部的には、マップのキーは常に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));

これにより、ペアのタイプを書き出す必要がなくなり、一般的に読みやすくなります。(特に長いタイプ名が登場する場合。)

于 2010-04-15T05:49:29.220 に答える
5

less than演算子のシグネチャは、である必要がありますbool operator<(const _pos& other) const。そうでない場合、このメンバー関数はnon-constとして宣言されているため、mapはconst関数でこの演算子を使用できません。

于 2010-04-15T05:48:54.697 に答える
4

演算子<の定義が間違っていると思います-右側(この場合は引数)はconstとマークされ、constメンバー関数である必要があります。

    bool operator<(const _pos& other) const{ 

            return this->xi < other.xi; 
    } 
于 2010-04-15T05:50:57.510 に答える