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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:09:21+00:00 2026-06-12T17:09:21+00:00

I’m trying to create a boost::function that allows setting of a member variable of

  • 0

I’m trying to create a boost::function that allows setting of a member variable of an object. I’ve created the simplest example I can think of to get across what I’m trying (and failing) to do. I feel like I have a grasp of boost::bind, but I’m new to boost and I believe I’m using boost::function incorrectly.

#include <iostream>
#include <Boost/bind.hpp>
#include <boost/function.hpp>

class Foo
{
public:
    Foo() : value(0) {}

    boost::function<void (int&)> getFcn()
    {
        return boost::function<void (int&)>( boost::bind<void>( Foo::value, this, _1 ) );
    }

    int getValue() const    { return value; }

private:
    int value;
};

int main(int argc, const char * argv[])
{
    Foo foo;

    std::cout << "Value before: " << foo.getValue();

    boost::function<void (int&)> setter = foo.getFcn();
    setter(10);     // ERROR: No matching function for call to object of type 'boost::function<void (int &)>'
                    // and in bind.hpp: Called object type 'int' is not a function or function pointer

    std::cout << "Value after: " << foo.getValue();

    return 0;
}

I’m having the error on line 28, where I want to use the function to set Foo::value to 10. Am I just going about this whole thing wrong? Should I just be passing back an int* or something instead of using boost for all of this? The reason I’m calling ‘getFcn()’ is because in my actual project I’m using a messaging system, and if the object with the data I want is no longer there, getFcn would return an empty boost::function. But I guess with an int* I could just return NULL if nothing was found.

  • 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-12T17:09:23+00:00Added an answer on June 12, 2026 at 5:09 pm

    This boost::bind<void>( Foo::value, this, _1 ) in your code is essentially using Foo::value as a member function. Which is wrong. Foo::value is not a function.

    Lets build this step by step:

    class Foo
    {
        ...
        boost::function< void (Foo*, int) > getFcn ()
        {
            return boost::function< void (Foo*, int) >( &Foo::setValue );
        }
    
        void setValue (int v)
        {
            value = v;
        }
        ...
    }
    
    int main ()
    {
        ...
        boost::function< void (Foo*, int) > setter = foo.getFcn();
        setter( &foo, 10);
        ...
    }
    

    So here the function takes this object explicitly. Let’s use boost.bind to bind this as first parameter.

    class Foo
    {
        ...
        boost::function< void (int) > getFcn ()
        {
            return boost::bind(&Foo::setValue, this, _1);
        }
    
        void setValue (int v)
        {
            value = v;
        }
        ...
    }
    
    int main ()
    {
        ...
        boost::function< void (int) > setter = foo.getFcn();
        setter( 10);
        ...
    }
    

    (untested code)

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

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.