Thread: Correct way to call boost::bind?

  1. #1
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005

    Correct way to call boost::bind?

    I'm trying to call boost::bind to create a function pointer to pass along to another function of mine.
    However, when I make an attempt to do so, it fails miserably.
    Here's how I'm doing it(this is the line causing the errors, if I comment it out, it compiles perfectly)
    merc.connect_bcastCurId(boost::bind(&MusicInfo::update_currentSongTitle, this, boost::cref(merc), _1));
    I'm trying to pass along the update_currentSongTitle function in my MusicInfo class, and also pass along a constant pointer to another object of mine, as the first argument in the update_currentSongTitle function.
    Here's my prototype:
    bool update_currentSongTitle(Merc const& merc, unsigned int const& id);
    And here's my prototype for the function that takes the function pointer:
    void connect_bcastCurId(boost::function<bool(Merc const&, unsigned int const&)> fp);
    Here are my errors(I pastebin'ed them because they messed up the formatting if I pasted them into code tags):

    Any help is appreciated, if you need more information, just ask and I'll provide it.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    You already supply an argument for merc, so the resulting bind object has the signature bool (unsigned int const&) (Side note: Why pass an int by const reference? By value is better for primitives.), which of course cannot bind to a function object with the signature bool (Merc const&, unsigned int const&).

    Either change the connect_bcastCurId prototype or use another placeholder in the binder.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get RSSI value, send to sensor, sensor receive package, repackage it?
    By techissue2008 in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-04-2009, 10:13 AM
  2. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  3. my HTTP request handler code correct?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-25-2008, 04:01 AM
  4. call base class function or derived class function
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2008, 05:23 AM
  5. Call external C programs and receive data
    By ferenczi in forum C Programming
    Replies: 11
    Last Post: 02-12-2008, 12:32 PM