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 8537079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:48:46+00:00 2026-06-11T10:48:46+00:00

I want to use a library (nlopt) that has a function set_min_objective which takes

  • 0

I want to use a library (nlopt) that has a function set_min_objective which takes a pointer to a numerical function myfunc and find its minimum. I would like to create a class that will contain a suitably initialized member function. set_min_objective would then find an optimum in a specific instance (myP in the example below). The call sequence is:

opt.set_min_objective(myfunc, NULL);

and I would like to use something like:

opt.set_min_objective(myP.f, NULL);

the error I get when compiling this is:

main.cpp: In function 'int main()':
main.cpp:79:34: error: no matching function for call to 'nlopt::opt::set_min_objective(<unresolved overloaded function type>, NULL)'
../lib/nlopt.hpp:335:10: note: candidates are: void nlopt::opt::set_min_objective(double (*)(unsigned int, const double*, double*, void*), void*)
../lib/nlopt.hpp:342:10: note:                 void nlopt::opt::set_min_objective(double (*)(const std::vector<double>&, std::vector<double>&, void*), void*)
../lib/nlopt.hpp:368:10: note:                 void nlopt::opt::set_min_objective(double (*)(unsigned int, const double*, double*, void*), void*, void* (*)(void*), void* (*)(void*))

What would be the simplest solution to have set_min_objective accepts myP.f as a normal pointer to function ? Note that myP.f and myfunc have the same arguments and return value types.

Thank you,

JD

  • 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-06-11T10:48:47+00:00Added an answer on June 11, 2026 at 10:48 am

    You can’t do that directly. You are trying to pass a pointer-to-member-function as a pointer-to-function. The way to go is to write a wrapper function (a normal function) which, for example, delegates the calculation to member function of your object, which is, for example, a global object.

    EDIT

    Actually, you are lucky: the second parameter to opt.set_min_objective is a pointer to your data which will be passed to the function you pass pointer to as first parameter. This means that your wrapper function doesn’t have to use a global object.

    class YourClass
    {
    public:
        double f(unsigned int i, const double*, double*, void*);
    };
    
    double wrapper(unsigned int i, const double* a, double* b, void* o) {
        // not sure what you'd need the last param for, but you say you have it...
        reinterpet_cast<YourClass*>(o)->f(i, a, b, o);
    }
    

    and then:

    YourClass myP;
    opt.set_min_objective(wrapper, &myP);
    

    Another edit. Someone else had a similar problem before you: http://www.cplusplus.com/forum/general/73166/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use an 3rd party library without using its header file. My
Recently I found a C library that I want to use in my C++
I have project in which I want to use check library for unit testing.
I want to use AntiXss library function in my web application. I have a
I want to use ASIO library from Boost in my project. Its doc say
I am making an app in which i want to use ffmpeg library.I have
I want to use a library developed by someone else, of which I only
I want to use a library that makes heavy use of singletons, but I
I found this open-source library that I want to use in my Java application.
I want to use a library that uses file descriptors as the basic means

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.