Note that there are some explanatory texts on larger screens.

plurals
  1. PO<unresolved overloaded function type> when trying to pass an aggregated object's method to its class method
    text
    copied!<p>I have some problem compiling my code. I have the following structure:</p> <pre><code>#include &lt;cstdlib&gt; using namespace std; typedef double (*FuncType)(int ); class AnotherClass { public: AnotherClass() {}; double funcAnother(int i) {return i*1.0;} }; class MyClass { public: MyClass(AnotherClass &amp; obj) { obj_ = &amp;obj;}; void compute(FuncType foo); void run(); protected: AnotherClass * obj_; /*pointer to obj. of another class */ }; void MyClass::compute(FuncType foo) { int a=1; double b; b= foo(a); } void MyClass::run() { compute(obj_-&gt;funcAnother); } /* * */ int main(int argc, char** argv) { AnotherClass a; MyClass b(a); b.run(); return 0; } </code></pre> <p>When I try to compile it, it gives:</p> <pre><code>main.cpp:39:31: error: no matching function for call to ‘MyClass::compute(&lt;unresolved overloaded function type&gt;)’ main.cpp:30:6: note: candidate is: void MyClass::compute(double (*)(int)) </code></pre> <p>What's wrong here?</p> <p>p/s/ <code>AnotherClass * obj_;</code> should stay like that because I write some function to the big library and can't change it.</p> <p>-------------- working version by Benjamin -------</p> <pre><code>#include &lt;cstdlib&gt; using namespace std; class AnotherClass { public: AnotherClass() {}; double funcAnother(int i) {return i*1.0;} }; struct Foo { /*constructor*/ Foo(AnotherClass &amp; a) : a_(a) {}; double operator()(int i) const { return a_.funcAnother(i); } AnotherClass &amp; a_; }; class MyClass { public: MyClass(AnotherClass &amp; obj) { obj_ = &amp;obj;}; template&lt;typename FuncType&gt; void compute(FuncType foo); void run(); protected: AnotherClass * obj_; /*pointer to obj. of another class */ }; template&lt;typename FuncType&gt; void MyClass::compute(FuncType foo) { int a=1; double b; b= foo(a); } void MyClass::run() { Foo f(*obj_); compute(f); } /* * */ int main(int argc, char** argv) { AnotherClass a; MyClass b(a); b.run(); return 0; } </code></pre> <p>Thank you everybody very much for the help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload