C++ std::set.insert-won';不能使用自定义类编译
我有一个类Record,它有三个私有整数字段、getter和setter,以及一个默认的和特定的构造函数。我打算用记录填充一个集合,但在让代码正常工作时遇到了问题C++ std::set.insert-won';不能使用自定义类编译,c++,class,insert,compilation,set,C++,Class,Insert,Compilation,Set,我有一个类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() {
#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());
}
#包括
使用名称空间std;
课堂记录
{
INTA、b、c;
公众:
//接球手和接球手
记录(){a=-1;b=-1;c=-1;};
}
int main()
{
设置s;
s、 插入(记录());
}
试图编译此错误的结果:
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
高级程度会考课程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::operator()(const\u Tp&,const\u Tp&)const[with_
Tp=记录]':
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_tree.h:1267:4:
仪表
从'std::pair,bool>std::_Rb_tree::_M_insert_unique(const _Val&)[带_Key
=Record,_Val=Record,_KeyOfValue=std::_Identity,_Compare=std::l
ess,_Alloc=std::分配器]'
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_set.h:410:29:
安装
从“std::pair”中指定,
_比较,typename\u Alloc::rebind::other>::const\u迭代器,bool>std::set
::插入(常量值类型&)[带_键=记录,_比较
e=std::less,_Alloc=std::分配器,typename std:_Rb_树
::const_iterator=std::_Rb_tree_const_iterator,std::set::value\u type=Record]'
a9.cpp:72:28:从此处实例化
c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_function.h:236:22:
呃
ror:与“operator”不匹配您需要重载
运算符您需要运算符我链接的Q&A似乎是关于std::map
(从标题中),但实际上是同一个问题(映射类型是std::set
的实例)。我在实际代码中的类定义后面有分号,很抱歉,我在这个简短的版本中遗漏了它。新的编译错误:x.cpp:在成员函数“bool记录:”中:operator@user2263849您的代码在哪里:Record::getOrderNum()?@user2263849您可以在这里看到代码编译正常:int getOrderNum(){return ordernum;}代码>用于有关后续注释的记录:int getOrdnerNum()const{return odernum;}代码>函数必须为常量。因为它似乎被操作符<函数使用,这也是常量。
bool operator<(const Record& rhs) const
{
return a < rhs.a; //assume that you compare the record based on a
}
class Record
{
int a, b, c;
public:
//getters and setters
Record(){a = -1; b = -1; c = -1;};
}; //<---Cannot miss this ;