当前位置:  开发笔记 > 编程语言 > 正文

C++.如何比较2个对象向量

如何解决《C++.如何比较2个对象向量》经验,为你挑选了1个好方法。

嗨我有2个对象向量,我想比较它们是否相等(表示v [i] == m [i]).我的班级有重载运算符==但我仍然有一个comp lire错误.

struct Node {
    Node() {};
    Node(int st, int end) : l(st), r(end) {
        if (st < 0 || end < st) {
            throw "Bad input";
        }
    }
    Node(int st, int end, int m, int s) : l(st), r(end), sum(s), min(m) {
        if (st < 0 || end < st) {
            throw "Bad input";
        }
    }

    bool operator== (Node& node) {
        if (node.l == l && node.r == r && node.sum == sum && node.min == min)
            return true;

        return false;
    }

    int l = 0, r = 0;
    int sum = 0;
    int min = 0;
};

int main() {
    vector segTree {} , exampleTree {};
    exampleTree.push_back(Node(0,0,1,1));

    if ( segTree ==  exampleTree )
        throw "smith";

    return 0;
}

错误:

In file included from test.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:216:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__locale:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:439:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:658:97: error: invalid operands to binary expression
      ('const Node' and 'const Node')
    _LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
                                                                                            ~~~ ^  ~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:1164:14: note: in instantiation of member function
      'std::__1::__equal_to::operator()' requested here
        if (!__pred(*__first1, *__first2))
             ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:1176:19: note: in instantiation of function template
      specialization 'std::__1::equal, std::__1::__wrap_iter, std::__1::__equal_to >' requested here
    return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
                  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:3192:41: note: in instantiation of function template
      specialization 'std::__1::equal, std::__1::__wrap_iter >' requested here
    return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
                                        ^
test.cpp:83:18: note: in instantiation of function template specialization 'std::__1::operator== >' requested here
    if ( segTree ==  exampleTree )
                 ^
test.cpp:66:6: note: candidate function not viable: 'this' argument has type 'const Node', but method is not marked const
bool operator== (Node& node) {
     ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:403:1: note: candidate template ignored: could not match
      'pair' against 'const Node'
operator==(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:574:1: note: candidate template ignored: could not match
      'reverse_iterator' against 'const Node'
operator==(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:866:6: note: candidate template ignored: could not match
      'istreambuf_iterator' against 'const Node'
bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
     ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:969:1: note: candidate template ignored: could not match
      'move_iterator' against 'const Node'
operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:1285:1: note: candidate template ignored: could not match
      '__wrap_iter' against 'const Node'
operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/tuple:877:1: note: candidate template ignored: could not match
      'tuple' against 'const Node'
operator==(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1791:6: note: candidate template ignored: could not match
      'allocator' against 'const Node'
bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
     ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2876:1: note: candidate template ignored: could not match
      'unique_ptr' against 'const Node'
operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2912:1: note: candidate template ignored: could not match
      'unique_ptr' against 'const Node'
operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2920:1: note: candidate template ignored: could not match
      'unique_ptr' against 'const Node'
operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4716:1: note: candidate template ignored: could not match
      'shared_ptr' against 'const Node'
operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4765:1: note: candidate template ignored: could not match
      'shared_ptr' against 'const Node'
operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4773:1: note: candidate template ignored: could not match
      'shared_ptr' against 'const Node'
operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
^
1 error generated.

MSalters.. 6

operator==应该采取一个const Node&,并成为const自己.你没有修改任何一个节点!



1> MSalters..:

operator==应该采取一个const Node&,并成为const自己.你没有修改任何一个节点!

推荐阅读
帆侮听我悄悄说星星
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有