Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
635 views
in Technique[技术] by (71.8m points)

c++ - <unresolved overloaded function type> when trying to pass an aggregated object's method to its class method

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...