std :: set.insert不会用自定义类编译 [英] std::set.insert won't compile with custom class

查看:193
本文介绍了std :: set.insert不会用自定义类编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,Record,有三个私有的整数字段,getter和setter,以及一个默认和特定的构造函数。我打算填充一个集合与记录,但我有问题,让代码工作。

I have a class, Record, with three private integer fields, getters and setters, and a default and specific constructor. I intend to populate a set with Records, but am having issues getting the code to work.

#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());
}

尝试在此错误中编译结果:

Attempting to compile results in this error:


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: \\ cw \ bin / gcc / mingw32 / 4.6.2 / include / c ++ / ios:43,
从c:\mingw\bin ../ lib / gcc / mingw32 / 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:
inst
从'std :: pair, bool> std :: _ Rb_tree< _Key,
_Val,_KeyOfValue,_Compare,_Alloc> :: _ M_insert_unique(const _Val&)[with _Key
= Record,_Val = Record,_KeyOfValue = std :: _ _Compare = std :: l
ess,_Alloc = std :: allocator]'
c:\mingw\bin ../ lib / gcc / mingw32 / 4.6.2 / include / c ++ / stl_set.h:410:29:
insta
从'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:
er
ror:'__x& __y'
c:\mingw\bin ../ lib / gcc / mingw32 / 4.6.2 / include / c ++ / bits / stl_function.h:236:22:
no
:candidate are:
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& co; b $ b nst std :: pair< _T1,_T2&)
c:\ \bin ../ lib / gcc / mingw32 / 4.6.2 / include / c ++ / bits / stl_iterator.h:291:5:
不是
e:template bool std :: operator< :: 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:
不是
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:
no
te:template 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:
no
te:template 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:
no
te:template 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:
note
:template 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:
note:
template bool std :: operator<(const std :: list< _Tp,_Allo
c>& const std :: list< _Tp,_Alloc>&)
c:\mingw\bin .. /lib/gcc/mingw32/4.6.2/include/c++/bits/stl_tree.h:856:5:
note: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:\ \\bin ../ lib / gcc / mingw32 / 4.6.2 / include / c ++ / bits / stl_set.h:713:5:
注意:te
mplate bool std :: operator< ::
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:
不是
e:template bool std :: operator<(const
std :: multiset& _Compare,_Alloc>& const std :: multiset< _Key,_Compare,_All
oc>&)

C:\Users\Denton\Documents\Indiana University\Class Documents\Spring 2013\CSCI-H2 12\Assignment9>g++ a9.cpp -o a9 In file included from c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/string:5 0:0, from c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/loc ale_classes.h:42, from c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/ios _base.h:43, from c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/ios:43, from c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/ostream: 40, from c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/iostream :40, from a9.cpp:3: c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_function.h: In member function '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: inst antiated from 'std::pair, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = Record, _Val = Record, _KeyOfValue = std::_Identity, _Compare = std::l ess, _Alloc = std::allocator]' c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_set.h:410:29: insta ntiated from '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: instantiated from here c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_function.h:236:22: er ror: no match for 'operator<' in '__x < __y' c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_function.h:236:22: no te: candidates are: c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_pair.h:207:5: note: t emplate bool std::operator<(const std::pair<_T1, _T2>&, co nst std::pair<_T1, _T2>&) c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_iterator.h:291:5: not e: template 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: no te: template 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: no te: template 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: no te: template 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: note : template 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: note: template bool std::operator<(const std::list<_Tp, _Allo c>&, const std::list<_Tp, _Alloc>&) c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_tree.h:856:5: note: 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: note: te mplate 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>&)

插入工作正常,当我做一个集合和s.insert(1)
这个代码需要做什么编译?

insert works fine when I make a set s and s.insert(1) What needs to be done for this code to compile?

感谢提前。

推荐答案

您需要重载运算符<
类似如下:

You need to overload the operator< : something like the following:

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

原因是STL set维护元素上的顺序。 std :: set支持指定比较函数。默认值为 less ,它将使用运算符< 来检查是否相等。

The reason is that STL set maintains order on elements. std::set supports specifying a comparison function. The default is less which will use operator < to check equality.

同时,你已经结束类定义; 否则编译错误。

Meanwhile, you have end you class definition with ; otherwise, compile error.

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

这篇关于std :: set.insert不会用自定义类编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆