5

我有一个类 Record,它具有三个私有整数字段、getter 和 setter,以及一个默认和特定的构造函数。我打算用记录填充一组,但在使代码正常工作时遇到问题。

#include <set>
using namespace std;

class Record
{
    int a, b, c;
    public:
    //getters and setters
    Record(){a = -1; b = -1; c = -1;};
}

int main()
{
    set< Record > s;
    s.insert(Record());
}

尝试编译会导致此错误:

C:\Users\Denton\Documents\Indiana University\Class Documents\Spring 2013\CSCI-H2 12\Assignment9>g++ a9.cpp -o a9 在包含在 c:\mingw\bin../lib/gcc/mingw32 的文件中/4.6.2/include/c++/string:5 0:0,来自 c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/loc ale_classes.h:42,来自c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/ios _base.h:43,来自 c:\mingw\bin../lib/gcc/mingw32/4.6。 2/include/c++/ios:43, 来自 c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/ostream: 40, 来自 c:\mingw\bin../lib/ gcc/mingw32/4.6.2/include/c++/iostream :40,来自 a9.cpp:3: c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_function。 h:在成员函数中'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _ Tp = Record]': c:\mingw\bin../lib/gcc/mingw32/4.6.2/include /c++/bits/stl_tree.h:1267:4: 从 'std::pair, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key =记录,_Val = 记录,_KeyOfValue = std::_Identity,_Compare = std::less,_Alloc = std::allocator]' c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/ c++/bits/stl_set.h:410:29: 从 'std::pair, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set <_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = Record, _Compar e = std::less, _Alloc = std::allocator, typename std::_Rb_tree<_ Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other> ::const_iterator = std::_Rb_tree_const_iterator, std::set<_Key, _Compare , _Alloc>: :value_type = Record]' a9.cpp:72:28: 从这里实例化 c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_function.h:236:22:错误:在 '__x < __y' c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_function.h:236:22 中与 'operator<' 不匹配:否te:候选人是:c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_pair.h:207:5:注意:t emplate bool std::operator<(const std::pair<_T1, _T2>&, 常量 std::pair<_T1, _T2>&) c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_iterator .h:291:5:不是 e: 模板 bool std::operator<(const std::reverse_iterator<_It erator>&, const std::reverse_iterator<_Iterator>&) c:\mingw\bin../lib/gcc/mingw32/4.6.2 /include/c++/bits/stl_iterator.h:341:5: not e: template bool std::operator<(const std::r everse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&) c:\ mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2510:5:注意:模板 bool std::operator<(cons t std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _ Traits, _Alloc>&) c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h: 2522:5:注意:模板 bool std::operator<(cons t std::basic_string<_CharT, _Traits, _Alloc>&,const _CharT*) c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2534:5:注意:模板 bool std::operator<(cons t _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&) c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:1290:5:注意:模板 bool std::operator<(const std::vector<_Tp, _ Alloc>&, const std::vector<_Tp, _Alloc>&) c:\mingw\bin../lib/gcc/mingw32/ 4.6.2/include/c++/bits/stl_list.h:1593:5:注意:模板 bool std::operator<(const std::list<_Tp, _Alloc>&, const std::list<_Tp, _Alloc >&) c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_tree.h:856:5: 注意:t emplate bool std::operator<(const std: :_Rb_tree<_Key, _Val, _KeyOfValue, _Compare,_All oc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&) c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_set。 h:713:5: 注意:模板 bool std::operator<(const std:: set<_Key, _Compare, _Alloc>&, const std::set<_Key, _Compare, _Alloc>&) c:\mingw\ bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_multiset.h:696:5: not e: template bool std::operator<(const std::multiset<_Key, _Compare, _Alloc >&, const std::multiset<_Key, _Compare, _All oc>&)const std::set<_Key, _Compare, _Alloc>&) c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_multiset.h:696:5: 不是 e:模板 bool std::operator<(const std::multiset<_Key, _Compare, _Alloc>&, const std::multiset<_Key, _Compare, _All oc>&)const std::set<_Key, _Compare, _Alloc>&) c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_multiset.h:696:5: 不是 e:模板 bool std::operator<(const std::multiset<_Key, _Compare, _Alloc>&, const std::multiset<_Key, _Compare, _All oc>&)

当我制作 set s 和 s.insert(1) 时插入工作正常 编译这段代码需要做什么?

提前致谢。

4

1 回答 1

18

您需要重载operator<: ,如下所示:

bool operator<(const Record& rhs) const 
{
   return a < rhs.a;  //assume that you compare the record based on a
}

原因是 STL 集维护元素的顺序。std::set 支持指定比较函数。默认值less将用于operator <检查相等性。

同时,你必须结束你的类定义,;否则编译错误。

class Record
{
   int a, b, c;
   public:
   //getters and setters
   Record(){a = -1; b = -1; c = -1;};
}; //<---Cannot miss this ;
于 2013-04-09T22:52:34.467 回答