2012-04-14 136 views
5

當我從這個片段的最後一行添加到我的程序代碼:GCC-4.7編譯錯誤

typedef std::set<Job> JobSet; 
typedef boost::shared_ptr<JobSet> JobSetPtr; 
JobSetPtr jobs_; 

jobs_->insert (job); // line 60 

我得到這個錯誤:

g++-4.7 -o /home/kron/Software/Synchronizer/1.0/Main.o -c src/Main.cpp 
In file included from /usr/include/c++/4.7/string:50:0, 
       from /usr/include/c++/4.7/bits/locale_classes.h:42, 
       from /usr/include/c++/4.7/bits/ios_base.h:43, 
       from /usr/include/c++/4.7/ios:43, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Main.cpp:1: 
/usr/include/c++/4.7/bits/stl_function.h: In instantiation of ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Job]’: 
/usr/include/c++/4.7/bits/stl_tree.h:1285:4: required from ‘std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = Job; _Val = Job; _KeyOfValue = std::_Identity<Job>; _Compare = std::less<Job>; _Alloc = std::allocator<Job>]’ 
/usr/include/c++/4.7/bits/stl_set.h:415:29: required from ‘std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = Job; _Compare = std::less<Job>; _Alloc = std::allocator<Job>; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator<Job>; std::set<_Key, _Compare, _Alloc>::value_type = Job]’ 
src/Configuration.hpp:60:27: required from here 
/usr/include/c++/4.7/bits/stl_function.h:236:22: error: no match for ‘operator<’ in ‘__x < __y’ 
/usr/include/c++/4.7/bits/stl_function.h:236:22: note: candidates are: 
In file included from /usr/include/c++/4.7/bits/stl_algobase.h:65:0, 
       from /usr/include/c++/4.7/bits/char_traits.h:41, 
       from /usr/include/c++/4.7/ios:41, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Main.cpp:1: 
/usr/include/c++/4.7/bits/stl_pair.h:218:5: note: template<class _T1, class _T2> bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) 
/usr/include/c++/4.7/bits/stl_pair.h:218:5: note: template argument deduction/substitution failed: 
In file included from /usr/include/c++/4.7/string:50:0, 
       from /usr/include/c++/4.7/bits/locale_classes.h:42, 
       from /usr/include/c++/4.7/bits/ios_base.h:43, 
       from /usr/include/c++/4.7/ios:43, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Main.cpp:1: 
/usr/include/c++/4.7/bits/stl_function.h:236:22: note: ‘const Job’ is not derived from ‘const std::pair<_T1, _T2>’ 
In file included from /usr/include/c++/4.7/bits/stl_algobase.h:68:0, 
       from /usr/include/c++/4.7/bits/char_traits.h:41, 
       from /usr/include/c++/4.7/ios:41, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Main.cpp:1: 
/usr/include/c++/4.7/bits/stl_iterator.h:299:5: note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&) 
/usr/include/c++/4.7/bits/stl_iterator.h:299:5: note: template argument deduction/substitution failed: 
In file included from /usr/include/c++/4.7/string:50:0, 
       from /usr/include/c++/4.7/bits/locale_classes.h:42, 
       from /usr/include/c++/4.7/bits/ios_base.h:43, 
       from /usr/include/c++/4.7/ios:43, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Main.cpp:1: 
/usr/include/c++/4.7/bits/stl_function.h:236:22: note: ‘const Job’ is not derived from ‘const std::reverse_iterator<_Iterator>’ 
In file included from /usr/include/c++/4.7/bits/stl_algobase.h:68:0, 
       from /usr/include/c++/4.7/bits/char_traits.h:41, 
       from /usr/include/c++/4.7/ios:41, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Main.cpp:1: 
/usr/include/c++/4.7/bits/stl_iterator.h:349:5: note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&) 
/usr/include/c++/4.7/bits/stl_iterator.h:349:5: note: template argument deduction/substitution failed: 
In file included from /usr/include/c++/4.7/string:50:0, 
       from /usr/include/c++/4.7/bits/locale_classes.h:42, 
       from /usr/include/c++/4.7/bits/ios_base.h:43, 
       from /usr/include/c++/4.7/ios:43, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Main.cpp:1: 
/usr/include/c++/4.7/bits/stl_function.h:236:22: note: ‘const Job’ is not derived from ‘const std::reverse_iterator<_IteratorL>’ 
In file included from /usr/include/c++/4.7/string:54:0, 
       from /usr/include/c++/4.7/bits/locale_classes.h:42, 
       from /usr/include/c++/4.7/bits/ios_base.h:43, 
       from /usr/include/c++/4.7/ios:43, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Main.cpp:1: 
/usr/include/c++/4.7/bits/basic_string.h:2566:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&) 
/usr/include/c++/4.7/bits/basic_string.h:2566:5: note: template argument deduction/substitution failed: 
In file included from /usr/include/c++/4.7/string:50:0, 
       from /usr/include/c++/4.7/bits/locale_classes.h:42, 
       from /usr/include/c++/4.7/bits/ios_base.h:43, 
       from /usr/include/c++/4.7/ios:43, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Main.cpp:1: 
/usr/include/c++/4.7/bits/stl_function.h:236:22: note: ‘const Job’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’ 
In file included from /usr/include/c++/4.7/string:54:0, 
       from /usr/include/c++/4.7/bits/locale_classes.h:42, 
       from /usr/include/c++/4.7/bits/ios_base.h:43, 
       from /usr/include/c++/4.7/ios:43, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Main.cpp:1: 
/usr/include/c++/4.7/bits/basic_string.h:2578:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) 
/usr/include/c++/4.7/bits/basic_string.h:2578:5: note: template argument deduction/substitution failed: 
In file included from /usr/include/c++/4.7/string:50:0, 
       from /usr/include/c++/4.7/bits/locale_classes.h:42, 
       from /usr/include/c++/4.7/bits/ios_base.h:43, 
       from /usr/include/c++/4.7/ios:43, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Main.cpp:1: 
/usr/include/c++/4.7/bits/stl_function.h:236:22: note: ‘const Job’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’ 
In file included from /usr/include/c++/4.7/string:54:0, 
       from /usr/include/c++/4.7/bits/locale_classes.h:42, 
       from /usr/include/c++/4.7/bits/ios_base.h:43, 
       from /usr/include/c++/4.7/ios:43, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Main.cpp:1: 
/usr/include/c++/4.7/bits/basic_string.h:2590:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&) 
/usr/include/c++/4.7/bits/basic_string.h:2590:5: note: template argument deduction/substitution failed: 
In file included from /usr/include/c++/4.7/string:50:0, 
       from /usr/include/c++/4.7/bits/locale_classes.h:42, 
       from /usr/include/c++/4.7/bits/ios_base.h:43, 
       from /usr/include/c++/4.7/ios:43, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Main.cpp:1: 
/usr/include/c++/4.7/bits/stl_function.h:236:22: note: mismatched types ‘const _CharT*’ and ‘Job’ 
In file included from /usr/include/c++/4.7/vector:65:0, 
       from src/Main.cpp:2: 
/usr/include/c++/4.7/bits/stl_vector.h:1372:5: note: template<class _Tp, class _Alloc> bool std::operator<(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&) 
/usr/include/c++/4.7/bits/stl_vector.h:1372:5: note: template argument deduction/substitution failed: 
In file included from /usr/include/c++/4.7/string:50:0, 
       from /usr/include/c++/4.7/bits/locale_classes.h:42, 
       from /usr/include/c++/4.7/bits/ios_base.h:43, 
       from /usr/include/c++/4.7/ios:43, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Main.cpp:1: 
/usr/include/c++/4.7/bits/stl_function.h:236:22: note: ‘const Job’ is not derived from ‘const std::vector<_Tp, _Alloc>’ 
In file included from /usr/include/c++/4.7/set:60:0, 
       from src/Configuration.hpp:4, 
       from src/Main.cpp:5: 
/usr/include/c++/4.7/bits/stl_tree.h:873:5: note: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator<(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&) 
/usr/include/c++/4.7/bits/stl_tree.h:873:5: note: template argument deduction/substitution failed: 
In file included from /usr/include/c++/4.7/string:50:0, 
       from /usr/include/c++/4.7/bits/locale_classes.h:42, 
       from /usr/include/c++/4.7/bits/ios_base.h:43, 
       from /usr/include/c++/4.7/ios:43, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Main.cpp:1: 
/usr/include/c++/4.7/bits/stl_function.h:236:22: note: ‘const Job’ is not derived from ‘const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>’ 
In file included from /usr/include/c++/4.7/set:61:0, 
       from src/Configuration.hpp:4, 
       from src/Main.cpp:5: 
/usr/include/c++/4.7/bits/stl_set.h:721:5: note: template<class _Key, class _Compare, class _Alloc> bool std::operator<(const std::set<_Key, _Compare, _Alloc>&, const std::set<_Key, _Compare, _Alloc>&) 
/usr/include/c++/4.7/bits/stl_set.h:721:5: note: template argument deduction/substitution failed: 
In file included from /usr/include/c++/4.7/string:50:0, 
       from /usr/include/c++/4.7/bits/locale_classes.h:42, 
       from /usr/include/c++/4.7/bits/ios_base.h:43, 
       from /usr/include/c++/4.7/ios:43, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Main.cpp:1: 
/usr/include/c++/4.7/bits/stl_function.h:236:22: note: ‘const Job’ is not derived from ‘const std::set<_Key, _Compare, _Alloc>’ 
In file included from /usr/include/c++/4.7/set:62:0, 
       from src/Configuration.hpp:4, 
       from src/Main.cpp:5: 
/usr/include/c++/4.7/bits/stl_multiset.h:702:5: note: template<class _Key, class _Compare, class _Alloc> bool std::operator<(const std::multiset<_Key, _Compare, _Alloc>&, const std::multiset<_Key, _Compare, _Alloc>&) 
/usr/include/c++/4.7/bits/stl_multiset.h:702:5: note: template argument deduction/substitution failed: 
In file included from /usr/include/c++/4.7/string:50:0, 
       from /usr/include/c++/4.7/bits/locale_classes.h:42, 
       from /usr/include/c++/4.7/bits/ios_base.h:43, 
       from /usr/include/c++/4.7/ios:43, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Main.cpp:1: 
/usr/include/c++/4.7/bits/stl_function.h:236:22: note: ‘const Job’ is not derived from ‘const std::multiset<_Key, _Compare, _Alloc>’ 
In file included from /usr/include/c++/4.7/list:64:0, 
       from /usr/include/boost/algorithm/string/std/list_traits.hpp:15, 
       from /usr/include/boost/algorithm/string/std_containers_traits.hpp:20, 
       from /usr/include/boost/algorithm/string.hpp:18, 
       from src/Configuration.hpp:9, 
       from src/Main.cpp:5: 
/usr/include/c++/4.7/bits/stl_list.h:1659:5: note: template<class _Tp, class _Alloc> bool std::operator<(const std::list<_Tp, _Alloc>&, const std::list<_Tp, _Alloc>&) 
/usr/include/c++/4.7/bits/stl_list.h:1659:5: note: template argument deduction/substitution failed: 
In file included from /usr/include/c++/4.7/string:50:0, 
       from /usr/include/c++/4.7/bits/locale_classes.h:42, 
       from /usr/include/c++/4.7/bits/ios_base.h:43, 
       from /usr/include/c++/4.7/ios:43, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Main.cpp:1: 
/usr/include/c++/4.7/bits/stl_function.h:236:22: note: ‘const Job’ is not derived from ‘const std::list<_Tp, _Alloc>’ 
In file included from /usr/include/c++/4.7/deque:65:0, 
       from /usr/include/boost/algorithm/string/find_format.hpp:14, 
       from /usr/include/boost/algorithm/string/replace.hpp:22, 
       from /usr/include/boost/algorithm/string.hpp:25, 
       from src/Configuration.hpp:9, 
       from src/Main.cpp:5: 
/usr/include/c++/4.7/bits/stl_deque.h:274:5: note: template<class _Tp, class _Ref, class _Ptr> bool std::operator<(const std::_Deque_iterator<_Tp, _Ref, _Ptr>&, const std::_Deque_iterator<_Tp, _Ref, _Ptr>&) 
/usr/include/c++/4.7/bits/stl_deque.h:274:5: note: template argument deduction/substitution failed: 
In file included from /usr/include/c++/4.7/string:50:0, 
       from /usr/include/c++/4.7/bits/locale_classes.h:42, 
       from /usr/include/c++/4.7/bits/ios_base.h:43, 
       from /usr/include/c++/4.7/ios:43, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Main.cpp:1: 
/usr/include/c++/4.7/bits/stl_function.h:236:22: note: ‘const Job’ is not derived from ‘const std::_Deque_iterator<_Tp, _Ref, _Ptr>’ 
In file included from /usr/include/c++/4.7/deque:65:0, 
       from /usr/include/boost/algorithm/string/find_format.hpp:14, 
       from /usr/include/boost/algorithm/string/replace.hpp:22, 
       from /usr/include/boost/algorithm/string.hpp:25, 
       from src/Configuration.hpp:9, 
       from src/Main.cpp:5: 
/usr/include/c++/4.7/bits/stl_deque.h:282:5: note: template<class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR> bool std::operator<(const std::_Deque_iterator<_Tp, _RefL, _PtrL>&, const std::_Deque_iterator<_Tp, _RefR, _PtrR>&) 
/usr/include/c++/4.7/bits/stl_deque.h:282:5: note: template argument deduction/substitution failed: 
In file included from /usr/include/c++/4.7/string:50:0, 
       from /usr/include/c++/4.7/bits/locale_classes.h:42, 
       from /usr/include/c++/4.7/bits/ios_base.h:43, 
       from /usr/include/c++/4.7/ios:43, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Main.cpp:1: 
/usr/include/c++/4.7/bits/stl_function.h:236:22: note: ‘const Job’ is not derived from ‘const std::_Deque_iterator<_Tp, _RefL, _PtrL>’ 
In file included from /usr/include/c++/4.7/deque:65:0, 
       from /usr/include/boost/algorithm/string/find_format.hpp:14, 
       from /usr/include/boost/algorithm/string/replace.hpp:22, 
       from /usr/include/boost/algorithm/string.hpp:25, 
       from src/Configuration.hpp:9, 
       from src/Main.cpp:5: 
/usr/include/c++/4.7/bits/stl_deque.h:1947:5: note: template<class _Tp, class _Alloc> bool std::operator<(const std::deque<_Tp, _Alloc>&, const std::deque<_Tp, _Alloc>&) 
/usr/include/c++/4.7/bits/stl_deque.h:1947:5: note: template argument deduction/substitution failed: 
In file included from /usr/include/c++/4.7/string:50:0, 
       from /usr/include/c++/4.7/bits/locale_classes.h:42, 
       from /usr/include/c++/4.7/bits/ios_base.h:43, 
       from /usr/include/c++/4.7/ios:43, 
       from /usr/include/c++/4.7/ostream:40, 
       from /usr/include/c++/4.7/iostream:40, 
       from src/Main.cpp:1: 
/usr/include/c++/4.7/bits/stl_function.h:236:22: note: ‘const Job’ is not derived from ‘const std::deque<_Tp, _Alloc>’ 
make: *** [/home/kron/Software/Synchronizer/1.0/Main.o] Error 1 

這是我的錯嗎?如果是,我做錯了什麼?如果不是,有人有相同的錯誤?

+1

你如何定義'工作'? – Shep 2012-04-14 17:10:58

+1

我不確定,但'Job'可能需要一個'operator <'過載。 – chris 2012-04-14 17:14:31

回答

10

看來你沒有提供<運營商的定義比較Job類型的兩個對象。

當您將對象插入到std::set中時,容器需要知道應該插入項目的位置,爲此需要將要插入的對象與已存在的項目進行比較。要做到這一點,它使用<運營商,我想你還沒有提供。 std::set<Job>根本不知道如何比較Job類型的兩個對象。

所以,你可以爲Job類提供operator<

,或者,定義該函子來比較Job對象:

struct JobComparer 
{ 
    bool operator()(Job const &j1, Job const &j2) 
    { 
     //compare j1 and j2 and return true or false 
    } 
}; 

然後用它作爲:

typedef std::set<Job, JobComparer> JobSet; 

或者,你可以專注std::lessexplained here

你可以看到這個話題:

+4

有時候,GCC錯誤日誌是超級無用的。感謝您的解釋。 – metamorphosis 2015-12-06 00:42:30

6

謝里夫提供了一個簡單的答案,但我認爲它值得更完整的一個。

對象的set,在C++中,與沒有重複的有序容器。爲了能夠排序對象,它使用除對象本身之外的第二個模板參數:一個函數,其作用是比較兩個對象。 它也使用第三個參數進行內存分配,這裏沒用。

當您使用set您提供兩個參數:

struct NameComparator { 
    template <typename T> 
    bool operator()(T const& left, T const& right) { 
     return left.name() < right.name(); 
    } 
}; 

std::set<Job, NameComparator> JobSet; 

提供應定義的順序一些概念比較。基本上,想怎麼<工作,爲兩個整數:

  • 是漫反射:x < x從不保存
  • 是反對稱:如果x < y然後y < x是假
  • 傳遞性:如果x < yy < z然後x < z

由於這是一種常見的關係,所以在標準庫中存在這樣的仿函數:template <typename T> struct std::less;,其工作s爲任何類型T對於給定的兩個實例,您可以使用運算符<

而且,您沒有明確指出的第二個參數set默認爲std::less<TypeOfObjectInSet>。這意味着只要提供了定義了<的對象,它就可以開箱即用。

因此,你有3個解決方案。

如果您的對象承認「自然」順序關係,那麼您可以定義operator<或專門std::less。否則,如果你只需要這個集合的一些訂單關係,那麼你可以提供你自己的比較器。

請注意,如果訂單是而不是需要,那麼您可以使用unordered_set。它需要散列和平等關係。

+0

+1瞭解詳情。 – Nawaz 2012-04-14 18:04:12