2013-05-01 72 views
0

我正在创建一个使用set来保存pair值的地图数据结构。我为这个程序制作了一个自定义的对班。我已经完成了大部分的调试工作,现在我正在使用xfunction类构建这些错误。我在类中构建的xfunction中出错,我不知道该怎么办

错误6错误C2784: '布尔的std ::操作<(常量的std :: basic_string的< _Elem,_Traits,_Alloc> &,常量_Elem *)':不能推导出模板参数的“常量的std :: basic_string的< _Elem,_Traits,_Alloc> & '从 '常量对' C:\程序文件(86)\微软的Visual Studio 10.0 \ VC \包括\ xfunctional 125

错误7错误C2784:' 布尔的std ::操作者<( const _Elem *,const std :: basic_string < _Elem,_Traits,_Alloc> &)':无法从'const Pair'c:\ program files(x86)\ microsoft visual studio 10.0 \中推导出'const _Elem *'的模板参数。 VC \包括\ xfunctional 125

错误8错误C2784: '布尔的std ::操作者<(常量的std :: basic_string的< _Elem,_Traits,_Alloc> &,常量性病:: basic_string的< _Elem,_Traits,_Alloc> &)' :不能推导出模板参数的 '常量的std :: basic_string的< _Elem,_Traits,_Alloc> &' 从 '常量对' C:\ Program Files文件(x86)的\微软的Visual Studio 10.0 \ VC \包括\ xfunctional 125

错误9错误C2784:'bool std :: operator <(const std :: move_iterator < _RanIt> &,const std :: move_iterator < _RanIt2> &)':无法为const const std :: move_iterator < _RanIt> &'从const对'推导出模板参数c:\ program files(x86)\ microsoft visual studio 10.0 \ vc \ include \ xfunctional 125

错误10错误C2784: '布尔的std ::操作<(常量的std :: _树< _Traits> &,常量的std :: _树< _Traits> &)':不能推导出模板参数的“常量的std :: _树< _Traits> &'from'const Pair'c:\ program files(x86)\ microsoft visual studio 10.0 \ vc \ include \ xfunctional 125

E rror 11 error C2784:'bool std :: operator <(const std :: list < _Ty,_Ax> &,const std :: list < _Ty,_Ax> &)':无法推导出'const std ::列表< _Ty,_AX> & '从 '常量对' C:\程序文件(86)\微软的Visual Studio 10.0 \ VC \包括\ xfunctional 125

错误12错误C2784:' 布尔的std ::操作者<(常量的std ::的unique_ptr < _Ty,_Dx> &,常量的std ::的unique_ptr < _Ty2,_Dx2> &)”:不能推导出模板参数 '常量的std ::的unique_ptr < _Ty,_Dx> &' 从 '常量配对' C:\亲克文件(x86)\ microsoft visual studio 10。0 \ VC \包括\ xfunctional 125

错误13错误C2784: '布尔的std ::操作者<(常量的std :: reverse_iterator的< _RanIt> &,常量性病:: reverse_iterator的< _RanIt2> &)':无法推断模板参数关于 '常量性病:: reverse_iterator的< _RanIt> &' 从 '常量对' C:\程序文件(86)\微软的Visual Studio 10.0 \ VC \包括\ xfunctional 125

错误14错误C2784:“布尔std :: operator <(const std :: _ Revranit < _RanIt,_Base> &,const std :: _ Revranit < _RanIt2,_Base2> ''从'const Pair'c:\ program files(x86)\ microsoft visual studio 10.0 \ vc \ include \ xfunctional 125

''无法为'const std :: _ Revranit推演模板参数'_RanIt,_Base> &'

错误15错误C2784: '布尔的std ::操作者<(常量的std ::对< _Ty1,_Ty2> &,常量性病::对< _Ty1,_Ty2> &)':无法推断出模板参数关于“常量性病:: pair < _Ty1,_Ty2> &'from'const Pair'c:\ program files(x86)\ microsoft visual studio 10.0 \ vc \ include \ xfunctional 125

Error 16 erro r 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"; 
    } 


} 

回答

1

问题是这样的:

error C2676:二进制<const Pair没有定义这个操作符或转换到类型接受的预先规定的操作

没有为Pair类的map2.h没有定义<操作者。 C++ STL集合类是使用平衡搜索树(如Red-black tree)实现的。该集合中的所有元素都存储在二叉树中。该集合通过将元素与树的根进行比较来检查它是否包含元素,然后在没有匹配的情况下仅递归到适当的子树中。这需要设置元素的比较运算符,以便确定树的哪一侧包含所寻找的元素。

该要求隐含在documentation for set中。

不幸的是,C++ STL编译器的消息通常是非常糟糕的无用事情。随着时间的推移,你会习惯它。有些东西你可以尝试去理解它们:

  • 只考虑包含你写的文件的错误消息,忽略关于标准库中文件的错误消息。这在这种情况下不会有帮助,但有时候会有帮助。
  • 专注于第一个或最后一个错误消息,并试图弄清这个问题
  • 尝试使用不同的编译器(如GCC或Clang)编译它以查看错误消息是否更有帮助。