gpt4 book ai didi

c++ - AppleClang : Operator Overloading Compilation Error

转载 作者:行者123 更新时间:2023-11-28 04:28:22 26 4
gpt4 key购买 nike

我按如下方式实现了运算符重载(参见方法 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代码编译正常,我只在 Mac OS X 上收到此错误使用标准平台编译器 ( AppleClang )。

我不明白为什么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 替换为直接成员访问。

关于c++ - AppleClang : Operator Overloading Compilation Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53686236/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com