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

  • Home
  • SEARCH
  • 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 805997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:10:32+00:00 2026-05-15T00:10:32+00:00

#include <boost/bind.hpp> #include <iostream> using namespace std; using boost::bind; class A { public: void

  • 0
#include <boost/bind.hpp>
#include <iostream>

using namespace std;
using boost::bind;

class A {
public:
    void print(string &s) {
        cout << s.c_str() << endl;
    }
};


typedef void (*callback)();

class B {
public:
    void set_callback(callback cb) {
        m_cb = cb;
    }

    void do_callback() {
        m_cb();
    }

private:
    callback m_cb;
};

void main() {
    A a;
    B b;
    string s("message");
    b.set_callback(bind(A::print, &a, s));
    b.do_callback();

}

So what I’m trying to do is to have the print method of A stream “message” to cout when b’s callback is activated. I’m getting an unexpected number of arguments error from msvc10. I’m sure this is super noob basic and I’m sorry in advance.

  • 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-15T00:10:32+00:00Added an answer on May 15, 2026 at 12:10 am

    replace typedef void (*callback)(); with typedef boost::function<void()> callback;

    A bound function doesn’t produce an ordinary function, so you cannot just store it in a regular function pointer. However, boost::function is able to handle anything as long as it is callable with the correct signature, so that’s what you want. It will work with a function pointer, or a functor created with bind.

    After a few corrections to your code, I came up with this:

    #include <boost/bind.hpp>
    #include <boost/function.hpp>
    #include <iostream>
    
    // i prefer explicit namespaces, but that's a matter of preference
    
    class A {
    public:
        // prefer const refs to regular refs unless you need to modify the argument!
        void print(const std::string &s) {
            // no need for .c_str() here, cout knows how to output a std::string just fine :-)
            std::cout << s << std::endl;
        }
    };
    
    
    // holds any arity 0 callable "thing" which returns void
    typedef boost::function<void()> callback;
    
    class B {
    public:
        void set_callback(callback cb) {
            m_cb = cb;
        }
    
        void do_callback() {
            m_cb();
        }
    
    private:
        callback m_cb;
    };
    
    void regular_function() {
        std::cout << "regular!" << std::endl;
    }
    
    // the return type for main is int, never anything else
    // however, in c++, you may omit the "return 0;" from main (and only main)
    // which will have the same effect as if you had a "return 0;" as the last line
    // of main
    int main() {
        A a;
        B b;
        std::string s("message");
    
        // you forget the "&" here before A::print!
        b.set_callback(boost::bind(&A::print, &a, s));
        b.do_callback();
    
        // this will work for regular function pointers too, yay!
        b.set_callback(regular_function);
        b.do_callback();
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <cstdlib> #include <iostream> #include <boost/bind.hpp> #include <boost/asio.hpp> using boost::asio::ip::tcp; class session { public:
#include <iostream> #include <set> #include <algorithm> #include <boost/lambda/lambda.hpp> #include <boost/bind.hpp> using namespace std; using
#include <boost/ptr_container/ptr_vector.hpp> #include <iostream> using namespace std; using namespace boost; struct A { ~A()
Compiling this example #include <boost/bind.hpp> #include <boost/algorithm/string.hpp> #include <algorithm> #include <iostream> #include <vector> using
#include <QtCore/QCoreApplication> #include <boost/bind.hpp> #include <boost/function.hpp> class button { public: boost::function<void()> onClick; boost::function<void(int ,double
This code: #include <boost/signals.hpp> #include <boost/bind.hpp> #include <boost/mem_fn.hpp> #include <iostream> class Recorder : public
I have the following code: #include <iostream> #include boost/shared_ptr.hpp using boost::shared_ptr; class Base {
Consider following example. #include <iostream> #include <algorithm> #include <vector> #include <boost/bind.hpp> void func(int e,
#include <iostream> #include <vector> #include <string> #include <ostream> #include <algorithm> #include <boost/function.hpp> #include <boost/bind.hpp>
#include <iostream> #include <algorithm> #include <vector> #include <boost/array.hpp> #include <boost/bind.hpp> int main() { boost::array<int,

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.