<unresolved overloaded function type> when trying to pass an aggregated object's method to its class method
Solution 1
The following function class will match the signature of your FuncType:
struct Foo
{
AnotherClass & a_;
Foo(AnotherClass & a) a_(a) {}
double operator()(int i) const
{
return a_.funcAnother(i);
}
};
Change MyClass::compute to a template, thusly:
template<typename FuncType>
void MyClass::compute(FuncType foo)
{
int a=1;
foo(a);
}
Then you can call run like this:
void MyClass::run()
{
compute(Foo(*obj_));
}
If your compiler supports lambdas (and there's a good chance it does), then you can forgo the function class and simply define run like this:
void MyClass::run()
{
auto f = [this](int i) {
return obj_->funcAnother(i);
};
compute(f);
}
Solution 2
Since,
funcAnother(int i);
is a member function it passes an implicit this
and then the prototype does not match the type of your function pointer.
The typedef for pointer to member function should be:
typedef double (AnotherClass::*funcPtr)(int);
Here is a modified compilable version of your code. Please check the comments inline to understand the changes, Also I left out the other details, you can add that up.
Denis
Updated on June 05, 2022Comments
-
Denis almost 2 years
I have some problem compiling my code. I have the following structure:
#include <cstdlib> using namespace std; typedef double (*FuncType)(int ); class AnotherClass { public: AnotherClass() {}; double funcAnother(int i) {return i*1.0;} }; class MyClass { public: MyClass(AnotherClass & obj) { obj_ = &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_->funcAnother); } /* * */ int main(int argc, char** argv) { AnotherClass a; MyClass b(a); b.run(); return 0; }
When I try to compile it, it gives:
main.cpp:39:31: error: no matching function for call to ‘MyClass::compute(<unresolved overloaded function type>)’ main.cpp:30:6: note: candidate is: void MyClass::compute(double (*)(int))
What's wrong here?
p/s/
AnotherClass * obj_;
should stay like that because I write some function to the big library and can't change it.-------------- working version by Benjamin -------
#include <cstdlib> using namespace std; class AnotherClass { public: AnotherClass() {}; double funcAnother(int i) {return i*1.0;} }; struct Foo { /*constructor*/ Foo(AnotherClass & a) : a_(a) {}; double operator()(int i) const { return a_.funcAnother(i); } AnotherClass & a_; }; class MyClass { public: MyClass(AnotherClass & obj) { obj_ = &obj;}; template<typename FuncType> void compute(FuncType foo); void run(); protected: AnotherClass * obj_; /*pointer to obj. of another class */ }; template<typename FuncType> 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; }
Thank you everybody very much for the help!