简体   繁体   English

“未解决的重载函数类型”错误,并将运算符作为函数参数传递

[英]“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.

相关问题 未解决的重载函数类型错误 - unresolved overloaded function type error 将函数指针传递给函数时处理<unresolved overloaded function type> - Dealing with <unresolved overloaded function type> when passing function pointer to function 的std ::线程 <unresolved overloaded function type> 错误 - std::thread <unresolved overloaded function type> error 编译错误:未解决的重载函数类型 - Compile error: unresolved overloaded function type 提升线程错误<unresolved overloaded function type> - boost thread error <unresolved overloaded function type> 尝试将函数作为参数传递时未解决的重载函数类型 - Unresolved overloaded function type when attempting to pass a function as an argument 类型为&#39;int&#39;和&#39;的无效操作数 <unresolved overloaded function type> &#39;转换为二进制&#39;operator &lt;&lt;&#39; - Invalid operands of types ‘int’ and ‘<unresolved overloaded function type>’ to binary ‘operator<<’ 错误:类型&#39;int&#39;和&#39;的操作数无效 <unresolved overloaded function type> &#39;到二元&#39;运算符&lt;&lt;&#39; - error: invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<' 错误:类型的无效操作数&#39; <unresolved overloaded function type> &#39;和&#39;int&#39;到二进制&#39;运算符&gt;&#39;if(min&gt; max) - error: invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator>' if(min > max) 为什么我会收到此错误:&#39;int&#39; 和 &#39; 类型的无效操作数<unresolved overloaded function type> &#39;转二进制&#39;运算符&lt;&lt;&#39; - Why am I getting this error: invalid operands of types ‘int’ and ‘<unresolved overloaded function type>’ to binary ‘operator<<’
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM