[英]“unresolved overloaded function type” error and passing an operator as a function argument
I'd would like to make a method that would perform a certain task plus make a simple calculation from within, like addition, subtraction or multiplication. 我想制作一个可以执行特定任务的方法,并从内部进行简单的计算,例如加法,减法或乘法。 Correct me if I'm wrong, it seems that I cannot pass the operator of such an operation directly and I need to define an intermediary method (like one in my example called operator_add ).
如果我错了,请纠正我,似乎我无法直接传递此类操作的运算符,而我需要定义一个中间方法(例如在我的示例中称为operator_add的方法)。 I try to accomplish my task with the following code :
我尝试使用以下代码完成任务:
struct A {
typedef int T;
/* (...) */
struct element {
/* (...) */
inline T value() const { /* something simple */ };
element& comp_assign( const T r, T (*operation)(const T, const T) ) { // line # 40
T res = operation( value(), r );
return modif_aux( res );
} /* (...) */
inline T operator_add( const T a, const T b ) { return a + b; }
inline element& operator+=( const T r ) { return comp_assign( r, operator_add ); } // line # 64
};
};
But I get the following error : 但是我收到以下错误:
A.h:64: error: no matching function for call to ‘A::element::comp_assign(const int&, <unresolved overloaded function type>)’
A.h:40: note: candidates are: A::element& A::element::comp_assign(int, int (*)(int, int))
operator_add
is a member function and so you can't use a normal function pointer to refer to it. operator_add
是成员函数,因此您不能使用普通的函数指针来引用它。 Making it a static function or free function would fix this, although I'll recommend using templates instead as then it can use any callable object: 将其设置为静态函数或自由函数可以解决此问题,尽管我建议您改用模板,因为它可以使用任何可调用对象:
template<typename Operation>
element& comp_assign( const T r, Operation operation) { // line # 40
T res = operation( value(), r );
return modif_aux( res );
}
inline element& operator+=( const T r ) { return comp_assign( r, std::plus<T>() ); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.