2013-04-09 3 views
1

레코드 세 개가 있으며, 세 개의 정수 필드, getter 및 setter 및 기본 및 특정 생성자가 있습니다. 레코드 세트를 채우려하고 있지만 코드를 작동시키는 데 문제가 있습니다.std :: set.insert가 사용자 정의 클래스로 컴파일되지 않습니다.

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

이 오류의 결과를 컴파일하려고 :이 코드를 컴파일하는 일련의과 s.insert (1) 어떻게 일을해야 할 때

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>&)

삽입 잘 작동?

미리 감사드립니다. 다음과 같은 일 :

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

이유는 그 STL 세트가 요소 순서를 유지하고있다

+6

'연산자 <'가 정의되어 있어야합니다. – Rapptz

+0

링크 된 Q & A는 (제목의)'std :: map'에 관한 것 같지만, 실제로 이것은 매우 똑같은 문제입니다 (매핑 된 타입은'std :: set <>'의 인스턴스입니다). –

답변

6

당신은 operator<를 오버로드해야합니다. std :: set는 비교 함수 지정을 지원합니다. 기본값은 less이며 operator <을 사용하여 일치 여부를 확인합니다.

한편, 클래스 정의를 ;으로 끝내면 컴파일 오류가 발생합니다.

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

내 실제 코드에서 세미콜론을 끝내고 있습니다. 죄송합니다.이 단축 버전에서는 여기를 생략했습니다. 새로 컴파일 오류 : 'x.cpp : 멤버 함수 '불리언 연산자 기록 :: <(녹음 및 CONST) CONST''' x.cpp : 30 : 38 : 오류로 : 'CONST 기록'을 통과 'this'인수가 'int Record :: g etOrderNum()'한정자 [-fpermissive]'를 버립니다. – user2263849

+0

@ user2263849 Record :: getOrderNum()? – taocp

+0

@ user2263849 당신은 코드가 여기에서 OK로 컴파일됨을 볼 수 있습니다 : http://liveworkspace.org/code/Ya3J3$0 – taocp

관련 문제