我有以下代码:

IndexedData index(strUrl);// IndexedData is a class.
jsonParser->parse(strTags,index,ret);

std::list<odIndexedDataNode> tags; // so index.tags is a STL list.

std::list<odIndexedDataNode> testListIdns;
insertTags(testListIdns);


这个testListIdns也是一个STL列表。我想比较index.tags和testListIdns是否相等。

我写了这个函数:

bool isEqual(std::list<odIndexedDataNode> &tags, std::list<odIndexedDataNode> &testListIdns) {
    if(tags.size() == testListIdns.size())
        return std::equal(tags.begin(), tags.end(), testListIdns.begin());
    else
        return false;
}


但是它给出了以下编译错误。有谁能够帮助我?

In file included from /usr/include/c++/4.6/bits/char_traits.h:41:0,
                 from /usr/include/c++/4.6/ios:41,
                 from /usr/include/c++/4.6/istream:40,
                 from /usr/include/c++/4.6/sstream:39,
                 from /root/target/include/boost/test/utils/wrap_stringstream.hpp:26,
                 from /root/target/include/boost/test/predicate_result.hpp:20,
                 from /root/target/include/boost/test/test_tools.hpp:19,
                 from /root/target/include/boost/test/unit_test.hpp:19,
                 from configuration_test.cpp:11:
/usr/include/c++/4.6/bits/stl_algobase.h: In static member function ‘static bool std::__equal<_BoolType>::equal(_II1, _II1, _II2) [with _II1 = std::_List_iterator<Rubicon::odIndexedDataNode>, _II2 = std::_List_iterator<Rubicon::odIndexedDataNode>, bool _BoolType = false]’:
/usr/include/c++/4.6/bits/stl_algobase.h:820:71:   instantiated from ‘bool std::__equal_aux(_II1, _II1, _II2) [with _II1 = std::_List_iterator<Rubicon::odIndexedDataNode>, _II2 = std::_List_iterator<Rubicon::odIndexedDataNode>]’
/usr/include/c++/4.6/bits/stl_algobase.h:1022:37:   instantiated from ‘bool std::equal(_II1, _II1, _II2) [with _II1 = std::_List_iterator<Rubicon::odIndexedDataNode>, _II2 = std::_List_iterator<Rubicon::odIndexedDataNode>]’
configuration_test.cpp:58:67:   instantiated from here
/usr/include/c++/4.6/bits/stl_algobase.h:791:6: error: no match for ‘operator==’ in ‘__first1.std::_List_iterator<_Tp>::operator* [with _Tp = Rubicon::odIndexedDataNode, std::_List_iterator<_Tp>::reference = Rubicon::odIndexedDataNode&]() == __first2.std::_List_iterator<_Tp>::operator* [with _Tp = Rubicon::odIndexedDataNode, std::_List_iterator<_Tp>::reference = Rubicon::odIndexedDataNode&]()’
/usr/include/c++/4.6/bits/stl_algobase.h:791:6: note: candidates are:
/usr/include/c++/4.6/bits/postypes.h:218:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
/usr/include/c++/4.6/bits/stl_pair.h:201:5: note: template<class _T1, class _T2> bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:285:5: note: template<class _Iterator> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:335:5: note: template<class _IteratorL, class _IteratorR> bool std::operator==(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/allocator.h:122:5: note: template<class _T1, class _T2> bool std::operator==(const std::allocator<_T1>&, const std::allocator<_T2>&)
/usr/include/c++/4.6/bits/allocator.h:127:5: note: template<class _Tp> bool std::operator==(const std::allocator<_Tp1>&, const std::allocator<_Tp1>&)
/usr/include/c++/4.6/bits/basic_string.h:2427: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.6/bits/basic_string.h:2434:5: note: template<class _CharT> typename __gnu_cxx::__enable_if<std::__is_char<_Tp>::__value, bool>::__type std::operator==(const std::basic_string<_CharT>&, const std::basic_string<_CharT>&)
/usr/include/c++/4.6/bits/basic_string.h:2448: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.6/bits/basic_string.h:2460: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.6/bits/streambuf_iterator.h:194:5: note: template<class _CharT, class _Traits> bool std::operator==(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)
/usr/include/c++/4.6/bits/stream_iterator.h:130:5: note: template<class _Tp, class _CharT, class _Traits, class _Dist> bool std::operator==(const std::istream_iterator<_Tp, _CharT, _Traits, _Dist>&, const std::istream_iterator<_Tp, _CharT, _Traits, _Dist>&)
/usr/include/c++/4.6/bits/stl_list.h:275:5: note: template<class _Val> bool std::operator==(const std::_List_iterator<_Tp>&, const std::_List_const_iterator<_Val>&)
/usr/include/c++/4.6/bits/stl_list.h:1564:5: note: template<class _Tp, class _Alloc> bool std::operator==(const std::list<_Tp, _Alloc>&, const std::list<_Tp, _Alloc>&)
/usr/include/c++/4.6/bits/stl_vector.h:1273:5: note: template<class _Tp, class _Alloc> bool std::operator==(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
/usr/include/c++/4.6/bits/stl_tree.h:309:5: note: template<class _Val> bool std::operator==(const std::_Rb_tree_iterator<_Tp>&, const std::_Rb_tree_const_iterator<_Val>&)
/usr/include/c++/4.6/bits/stl_tree.h:856: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.6/bits/stl_map.h:882:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator==(const std::map<_Key, _Tp, _Compare, _Alloc>&, const std::map<_Key, _Tp, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_multimap.h:800:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator==(const std::multimap<_Key, _Tp, _Compare, _Alloc>&, const std::multimap<_Key, _Tp, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_deque.h:246: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.6/bits/stl_deque.h:253: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.6/bits/stl_deque.h:1917:5: note: template<class _Tp, class _Alloc> bool std::operator==(const std::deque<_Tp, _Alloc>&, const std::deque<_Tp, _Alloc>&)
/usr/include/c++/4.6/bits/stl_stack.h:241:5: note: template<class _Tp, class _Seq> bool std::operator==(const std::stack<_Tp, _Seq>&, const std::stack<_Tp, _Seq>&)

最佳答案

看来您的类型odIndexedDataNode没有运算符==。您应该重载它,或编写函数/函数并将其作为第四个参数发送到std::equal

关于c++ - 比较C++中的两个STL列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29604564/

10-11 22:24