繁体   English   中英

AppleClang:运算符重载编译错误

[英]AppleClang: Operator Overloading Compilation Error

我实现了运算符重载,如下所示(请参见方法bool operator==bool operator< ):

#include "../data_types.h"

class OffsetValuePair {
private:
    unsigned_value offset;
    unsigned_value value;

public:
    OffsetValuePair(unsigned_value address, unsigned_value value) {
        this->offset = address;
        this->value = value;
    }

    bool operator==(const OffsetValuePair offsetValuePair) {
        return this->getOffset() == offsetValuePair.offset;
    }

    bool operator<(const OffsetValuePair offset_value_pair) {
        return this->getValue() < offset_value_pair.value;
    }

    unsigned_value getOffset() {
        return offset;
    }

    unsigned_value getValue() {
        return value;
    }
};

我正在使用运算符查找下限,而将使用我的重载运算符小于( < ):

const auto lower_bound_offset_value_pair = OffsetValuePair(0, 1234);
const auto lower_bound = std::lower_bound(pointer_map_sorted_by_value_.begin(),
                                                  pointer_map_sorted_by_value_.end(),
                                                  lower_bound_offset_value_pair);

编译代码时, AppleClang出现以下错误:

/Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:719:71: error: invalid operands to binary expression ('const OffsetValuePair' and 'const OffsetValuePair')
    bool operator()(const _T1& __x, const _T1& __y) const {return __x < __y;}
                                                                  ~~~ ^ ~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:4285:13: note: in instantiation of member function 'std::__1::__less<OffsetValuePair, OffsetValuePair>::operator()' requested here
        if (__comp(*__m, __value_))
            ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:4307:12: note: in instantiation of function template specialization 'std::__1::__lower_bound<std::__1::__less<OffsetValuePair, OffsetValuePair> &, std::__1::__wrap_iter<OffsetValuePair *>, OffsetValuePair>' requested here
    return __lower_bound<_Comp_ref>(__first, __last, __value_, __comp);
           ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm:4316:19: note: in instantiation of function template specialization 'std::__1::lower_bound<std::__1::__wrap_iter<OffsetValuePair *>, OffsetValuePair, std::__1::__less<OffsetValuePair, OffsetValuePair> >' requested here
    return _VSTD::lower_bound(__first, __last, __value_,
                  ^
/Users/bully/Desktop/PointerSearcher/src/pointer_search_objects/PointerSearcher.h:660:33: note: in instantiation of function template specialization 'std::__1::lower_bound<std::__1::__wrap_iter<OffsetValuePair *>, OffsetValuePair>' requested here
                const auto lower_bound = std::lower_bound(pointer_map_sorted_by_value_.begin(),
                                              ^
/Users/bully/Desktop/PointerSearcher/src/pointer_search_objects/OffsetValuePair.h:21:7: note: candidate function not viable: 'this' argument has type 'const OffsetValuePair', but method is not marked const
        bool operator<(const OffsetValuePair offset_value_pair) {
             ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/utility:572:1: note: candidate template ignored: could not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const OffsetValuePair'
operator< (const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/iterator:702:1: note: candidate template ignored: could not match 'reverse_iterator<type-parameter-0-0>' against 'const OffsetValuePair'
operator<(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/iterator:1143:1: note: candidate template ignored: could not match 'move_iterator<type-parameter-0-0>' against 'const OffsetValuePair'
operator<(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/iterator:1512:1: note: candidate template ignored: could not match '__wrap_iter<type-parameter-0-0>' against 'const OffsetValuePair'
operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT_DEBUG
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/tuple:1187:1: note: candidate template ignored: could not match 'tuple<type-parameter-0-0...>' against 'const OffsetValuePair'
operator<(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2920:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const OffsetValuePair'
operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2978:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const OffsetValuePair'
operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:2987:1: note: candidate template ignored: could not match 'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const OffsetValuePair'
operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:4758:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const OffsetValuePair'
operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:4823:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const OffsetValuePair'
operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/memory:4831:1: note: candidate template ignored: could not match 'shared_ptr<type-parameter-0-0>' against 'const OffsetValuePair'
operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT

使用gccMSVC ,代码可以很好地编译,我只在使用标准平台编译器( AppleClang )的Mac OS X上遇到此错误。

我不明白为什么AppleClang不接受该代码。 怎么了

我无法声明方法const因为它们使用this引用的方法调用。 如果我this对象转换为第二个方法参数,则会收到另一个错误,告诉我方法签名是错误的:错误: 'bool OffsetValuePair::operator<(OffsetValuePair, OffsetValuePair)' must have exactly one argument

没关系,我只是找到了解决方案:

bool operator==(const OffsetValuePair offsetValuePair) const {
    return this->offset == offsetValuePair.offset;
}

bool operator<(const OffsetValuePair offset_value_pair) const {
    return this->value < offset_value_pair.value;
}

所做的更改是将两种方法都定义为错误消息所建议的const 此外,由于使用const方法定义,因此在使用this引用方法时不允许调用,因此我将getter替换为直接成员访问。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM