0

set を使用してペア値を保持するマップ データ構造を作成しています。プログラム用のカスタム ペア クラスを作成しました。ほとんどのデバッグを完了しましたが、組み込みの xfunction クラスでこれらのエラーが発生しています。

エラー 6 エラー C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : 'const std::basic_string<_Elem,_Traits, _Alloc> &' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

エラー 7 エラー C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : 'const Pair' から 'const _Elem *' のテンプレート引数を推測できませんでした c :\プログラム ファイル (x86)\マイクロソフト ビジュアル スタジオ 10.0\vc\include\xfunctional 125

エラー 8 エラー C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : ' のテンプレート引数を推測できませんでしたconst std::basic_string<_Elem,_Traits,_Alloc> &' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

エラー 9 エラー C2784: 'bool std::operator <(const std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : 'const std::move_iterator<_RanIt> のテンプレート引数を推測できませんでした&' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

エラー 10 エラー C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : 'const std::_Tree<_Traits> のテンプレート引数を推測できませんでした&' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

エラー 11 エラー C2784: 'bool std::operator <(const std::list<_Ty,_Ax> &,const std::list<_Ty,_Ax> &)' : 'const std:: のテンプレート引数を推測できませんでした:: list<_Ty,_Ax> &' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

エラー 12 エラー C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : 'const std:: のテンプレート引数を推定できませんでした:: unique_ptr<_Ty,_Dx> &' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

エラー 13 エラー C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : 'const std::reverse_iterator<_RanIt> のテンプレート引数を推定できませんでした&' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

エラー 14 エラー C2784: 'bool std::operator <(const std::_Revrinit<_RanIt,_Base> &,const std::_Revrinit<_RanIt2,_Base2> &)' : 'const std:: のテンプレート引数を推測できませんでした:: _Revrinit<_RanIt,_Base> &' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

エラー 15 エラー C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : 'const std:: のテンプレート引数を推測できませんでした:: pair<_Ty1,_Ty2> &' 'const Pair' から c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

エラー 16 エラー C2676: バイナリ '<' : 'const Pair' は、この演算子を定義していないか、定義済みの演算子 c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125 に受け入れられる型への変換を定義していません

これがmap2.hの私のコードです

#ifndef MAP_H_2
#define MAP_H_2
#include <list>
#include <set>
#include <utility>
#include <iterator>
#include <iostream>
#include <string>
using namespace std;

//pair class header
template<typename F, typename S>
class Pair
{
public:
    Pair(const F& a, const S& b);
    Pair();
    F get_first() const;
    S get_second() const;
private:
    F first;
    S second;
};
//pair class definitions
template<typename F, typename S>
inline Pair<F, S>::Pair(const F& a, const S& b):first(a),second(b){}

template<typename F, typename S>
inline Pair<F, S>::Pair()
{
}

template<typename F, typename S>
inline F Pair<F, S>::get_first() const
{
    return first;
}

template<typename F, typename S>
inline S Pair<F, S>::get_second() const
{
    return second;
}
//map header
class map2
{
public:
    map2();
    void at_put(string key, int value);
    Pair<string, int> at(string key);
    bool contain_key(string key);
    int value_of(string key);
    void remove_key(string key);
    void print();

private:
    set<Pair<string, int>> theList;
};

//map definition
map2::map2(){}

void map2::at_put(string key, int value)
{
    bool notThere = true;
    if(contain_key(key))
    {
        notThere = false;
    }
    if(notThere)
    {
        Pair<string, int> thePair(key, value);
        theList.insert(thePair);
    }
}

Pair<string, int> map2::at(string key)
{
    set<Pair<string, int>>::iterator iter = theList.begin();
    Pair<string, int> thePair;
    string temp;
    for(int x = 0; x<theList.size() ; x++)
    {
        thePair = *iter;
        temp = thePair.get_first();
        if(!key.compare(temp))
        {
            return thePair;
        }
        iter++;
    }
    Pair<string, int> noPair = Pair<string, int>("none", -1);
    return noPair;
}

bool map2::contain_key(string key)
{
    set<Pair<string, int>>::iterator iter = theList.begin();
    Pair<string, int> thePair;
    string temp;
    for(int x = 0; x<theList.size() ; x++)
    {
        thePair = *iter;
        temp = thePair.get_first();
        if(!key.compare(temp))
        {
            return true;
        }
        iter++;
    }
    return false;
}

int map2::value_of(string key)
{
    set<Pair<string, int>>::iterator iter = theList.begin();
    Pair<string, int> thePair;
    string temp;
    for(int x = 0; x<theList.size() ; x++)
    {
        thePair = *iter;
        temp = thePair.get_first();
        if(!key.compare(temp))
        {
            return thePair.get_second();
        }
        iter++;
    }
    return NULL;
}

void map2::remove_key(string key)
{
    set<Pair<string, int>>::iterator iter = theList.begin();
    Pair<string, int> thePair;
    string temp;
    for(int x = 0; x<theList.size() ; x++)
    {
        thePair = *iter;
        temp = thePair.get_first();
        if(!key.compare(temp))
        {
            theList.erase(iter);
        }
        iter++;
    }
}

void map2::print()
{
    set<Pair<string, int>>::iterator iter = theList.begin();
    Pair<string, int> thePair;
    string temp;
    int temp2;
    for(int x = 0; x<theList.size() ; x++)
    {
        thePair = *iter;
        temp = thePair.get_first();
        temp2 = thePair.get_second();
        cout << "Key:" << temp << " Value:" << temp2 << "\n";
        iter++;
    }
}

#endif

これがメイン関数を持つ私のコードです

#include "map2.h"
#include <string>
#include <iostream>

using namespace std;


int main()
{
    map2 theMap;
    theMap.at_put("John", 1000);
    theMap.at_put("Chris", 1000);
    theMap.at_put("John", 1500);
    theMap.at_put("Bob", 1250);

    theMap.print();

    theMap.remove_key("bob");

    theMap.print();

    string findKey;
    cout << "please enter a key to remove" << "\n";
    cin >> findKey;

    bool keyFound = theMap.contain_key(findKey);

    if(keyFound)
    {
        cout << "We found it! The value is:" << theMap.value_of(findKey) << "\n";
    }
    else
    {
        cout << "we don’t have this key " << findKey << "in the map" << "\n";
    }


}
4

1 に答える 1

1

問題はこれです:

エラー C2676 : バイナリ<:const Pairこの演算子または事前定義された演算子に受け入れられる型への変換が定義されていません

のクラス<に対して定義された演算子はありません。C++ STL セット クラスは、 Red-black treeなどのバランスの取れた検索ツリーを使用して実装されます。セット内のすべての要素は、バイナリ ツリーに格納されます。セットは、要素をツリーのルートと比較することによって、要素が含まれているかどうかをチェックし、一致がない場合は、1 つの適切なサブツリーのみに再帰します。これには、ツリーのどちら側に検索対象の要素が含まれているかを判断するために、集合要素の比較演算子が必要です。Pairmap2.h

この要件は、 のドキュメントにset暗黙のうちに埋め込まれています。

残念ながら、C++ STL コンパイラのメッセージは通常、このような非常に役に立たないものです。時間が経つにつれて、あなたはそれに慣れるでしょう。それらを理解するために試すことができるいくつかのこと:

  • 自分が作成したファイルを含むエラー メッセージのみを考慮し、標準ライブラリ内のファイルに関するエラー メッセージは無視します。この特定のケースでは役に立ちませんでしたが、役立つ場合もあります。
  • 最初または最後のエラー メッセージに注目して、その意味を理解してください。
  • GCC や Clang などの別のコンパイラでコンパイルしてみて、エラー メッセージが多少役立つかどうかを確認してください。
于 2013-05-01T03:31:13.777 に答える