GCC-4.7编译错误

时间:2012-04-14 17:07:37

标签: c++ gcc compilation gcc4.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

这是我的错吗?如果是的话,我做错了什么?如果不是,有人有同样的错误吗?

2 个答案:

答案 0 :(得分:10)

您似乎没有提供<运算符的定义来比较Job类型的两个对象。

当您将对象插入std::set时,容器需要知道它应插入项目的位置,为此需要将要插入的对象与已存在的项目进行比较。组。为此,它使用<运算符,我想你没有提供。 std::set<Job>根本不知道如何比较Job类型的两个对象。

因此,您可以为operator<课程提供Job

或者,或者,定义此仿函数以比较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::less专门设为explained here

您可以看到此主题:

答案 1 :(得分:6)

Nawaz提供了一个简单的答案,但我认为它应该更完整。

C ++中的set个对象是一个没有重复的有序容器。为了能够对对象进行排序,它使用除对象本身之外的第二个模板参数:一个仿函数,其作用是比较两个对象。 它还使用第3个参数进行内存分配,这里没用。

当您使用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 < yy < x为假
  • 它具有传递性:如果x < yy < zx < z

由于这是一种常见的关系,因此标准库中存在这样的仿函数:template <typename T> struct std::less;,适用于任何类型T,对于给定的两个实例,您可以使用运算符{{1} }。

实际上,您未明确指出的<的第二个参数默认为set。这意味着当提供定义了std::less<TypeOfObjectInSet>的对象时,它只是开箱即用。

因此,您有3个解决方案。

如果您的对象允许“自然”订单关系,那么您可以定义<或专门化operator<。否则,如果您只需要此集合的某个订单关系,那么您可以提供自己的比较器。

请注意,如果订单,则可以使用std::less。它需要散列和相等的关系。