std::set.insert won#39;t compile with custom class(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:UsersDentonDocumentsIndiana UniversityClass DocumentsSpring2013CSCI-H212Assignment9>g++ a9.cpp -o a9在 c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/string:5 包含的文件中0:0,来自 c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/bits/locale_classes.h:42,来自 c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/bits/ios_base.h:43,来自 c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/ios:43,来自 c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/ostream:40,来自 c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/iostream:40,从 a9.cpp:3:c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_function.h:在会员function 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = 记录]':c:mingwin../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:mingwin../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, _Compare = std::less, _Alloc = std::allocator, 类型名 std::_Rb_tree<_键、_Key、std::_Identity<_Key>、_Compare、类型名 _Alloc::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator, std::set<_Key, _Compare, _Alloc>::value_type = 记录]'a9.cpp:72:28:从这里实例化c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_function.h:236:22:呃ror: 不匹配 'operator<'在'__x <__y'c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_function.h:236:22:不te:候选人是:c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_pair.h:207:5:注:吨emplate bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_iterator.h:291:5:不是e: 模板 bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_iterator.h:341:5:不是e: 模板 bool std::operator<(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2510:5:不te: 模板 bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _特征,_Alloc>&)c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2522:5:不te: 模板 bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2534:5:不te: 模板 bool std::operator<(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)c:mingwin../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:mingwin../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:mingwin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_tree.h:856:5:注:吨模板bool std::operator<(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&)c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_set.h:713:5:注:template bool std::operator<(const std::set<_Key, _Compare, _Alloc>&, const std::set<_Key, _Compare, _Alloc>&)c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_multiset.h:696:5:不是e: 模板 bool std::operator<(conststd::multiset<_Key, _Compare, _Alloc>&, const std::multiset<_Key, _Compare, _Alloc>&)
C:UsersDentonDocumentsIndiana UniversityClass DocumentsSpring 2013CSCI-H2 12Assignment9>g++ a9.cpp -o a9 In file included from c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/string:5 0:0, from c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/bits/loc ale_classes.h:42, from c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/bits/ios _base.h:43, from c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/ios:43, from c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/ostream: 40, from c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/iostream :40, from a9.cpp:3: c:mingwin../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:mingwin../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:mingwin../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:mingwin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_function.h:236:22: er ror: no match for 'operator<' in '__x < __y' c:mingwin../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_function.h:236:22: no te: candidates are: c:mingwin../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:mingwin../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:mingwin../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:mingwin../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:mingwin../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:mingwin../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:mingwin../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:mingwin../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:mingwin../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:mingwin../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:mingwin../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>&)
当我制作 set s 和 s.insert(1) 时,insert 工作正常这段代码需要做什么才能编译?
insert works fine when I make a set s and s.insert(1) What needs to be done for this code to compile?
提前致谢.
推荐答案
你需要重载 operator<
:类似于以下内容:
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 集合维护元素的顺序.std::set 支持指定比较函数.默认是 less
,它将使用 operator <
来检查相等性.
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 to 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 不会用自定义类编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:std::set.insert 不会用自定义类编译
基础教程推荐
- 设计字符串本地化的最佳方法 2022-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- C++,'if' 表达式中的变量声明 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01