2012-04-14 26 views
5

Khi tôi thêm dòng cuối cùng từ đoạn này sang mã chương trình của tôi:GCC-4.7 Compilation lỗi

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

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

tôi nhận được lỗi này:

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 

Có lỗi của tôi? Nếu có, tôi đã làm gì sai? Nếu không, ai đó có lỗi giống nhau không?

+1

làm cách nào để bạn xác định 'công việc'? – Shep

+1

Tôi không chắc chắn, nhưng 'Job' có thể cần một toán tử' operator <'. – chris

Trả lời

10

Có vẻ như bạn đã không cung cấp định nghĩa của nhà điều hành < để so sánh hai đối tượng thuộc loại Job.

Khi bạn chèn một đối tượng vào std::set, vùng chứa cần biết vị trí cần chèn mục, mà cần phải so sánh đối tượng cần chèn với các mục đã có trong bộ này. Để làm điều đó, nó sử dụng toán tử < mà tôi đoán bạn đã không cung cấp. std::set<Job> đơn giản là không biết cách so sánh hai đối tượng thuộc loại Job.

Vì vậy, bạn có thể cung cấp operator< cho lớp Job.

Hoặc cách khác, xác định functor này để so sánh Job đối tượng:

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

sau đó sử dụng nó như là:

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

Hoặc, bạn có thể chuyên std::less như explained here.

Bạn có thể xem chủ đề này:

+4

Các nhật ký lỗi của người dùng, GCC đôi khi vô cùng hữu ích. Cảm ơn lời giải thích. – metamorphosis

6

Nawaz cung cấp một câu trả lời đơn giản, nhưng tôi nghĩ rằng nó xứng đáng là một một hoàn chỉnh hơn.

A set đối tượng, trong C++, là vùng chứa có thứ tự không có trùng lặp. Để có thể sắp xếp các đối tượng, nó sử dụng tham số mẫu thứ hai ngoài chính đối tượng: một hàm functor có công việc là so sánh hai đối tượng. Nó cũng sử dụng tham số thứ 3 để cấp phát bộ nhớ, vô ích ở đây.

Khi bạn sử dụng một set bạn cung cấp hai thông số:

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

std::set<Job, NameComparator> JobSet; 

Các so sánh cung cấp cần xác định một số khái niệm về trật tự.Về cơ bản, suy nghĩ về cách < làm việc cho hai số nguyên:

  • nó là irreflexive: x < x không bao giờ nắm giữ
  • nó là antisymmetric: nếu x < y sau đó y < x là sai
  • nó là bắc cầu: nếu x < yy < z sau đó x < z

Vì đây là mối quan hệ phổ biến như vậy, nên hàm functor tồn tại trong thư viện chuẩn: template <typename T> struct std::less;, hoạt động s cho bất kỳ loại T nào với hai trường hợp nhất định, bạn có thể sử dụng toán tử <.

Và vì vậy, tham số thứ hai của set, mà bạn không chỉ định rõ ràng, mặc định là std::less<TypeOfObjectInSet>. Có nghĩa là nó chỉ hoạt động ra khỏi hộp khi được cung cấp với các đối tượng mà trong đó < được xác định.

Vì vậy, bạn có 3 giải pháp.

Nếu đối tượng của bạn thừa nhận quan hệ đặt hàng "tự nhiên", thì bạn có thể xác định operator< hoặc chuyên std::less. Nếu không, nếu bạn chỉ cần một số mối quan hệ đặt hàng cho bộ này thì bạn có thể cung cấp bộ so sánh của riêng bạn.

Lưu ý rằng nếu đặt hàng là không phải bắt buộc, thì bạn có thể sử dụng unordered_set. Nó đòi hỏi băm và mối quan hệ bình đẳng.

+0

+1 để biết chi tiết. – Nawaz

Các vấn đề liên quan