when trying to pass an aggregated obj

2019-01-27 02:48发布

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!

2条回答
【Aperson】
2楼-- · 2019-01-27 03:20

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.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-27 03:31

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);
}
查看更多
登录 后发表回答