Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6323135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:27:42+00:00 2026-05-24T16:27:42+00:00

I have some problem compiling my code. I have the following structure: #include <cstdlib>

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:27:43+00:00Added an answer on May 24, 2026 at 4:27 pm

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with some JNI code. I'm getting the following error printed
Here's my problem - I have some code like this: <mx:Canvas width=300 height=300> <mx:Button
I have an intermittent problem with some code that writes to a Windows Event
I have run into a bit of a tricky problem in some C++ code,
I have a problem with jQuqeryUI. I would like to run some code whenever
I welcome any help for the following code optimization problem: I have a collection
I discovered to have some problem to fully understand callbacks scoping when trying to
So I have run up against this problem a few times: I have some
I have problem in some JavaScript that I am writing where the Switch statement
I've some CSS problem in Firefox 3. I have several image buttons on my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.