No viable overloaded '=' in std::bind function

619 views Asked by At

I am trying to set the value of the referenced variable i_RootPath to different values inside of the while-loop, if you click on the corresponding button. The compiles does not like the way I assign i_RootPath. It says:

No viable overloaded '='

How can I successfully change the value of "i_RootPath" from within the different methods called by the buttons generated?

void NodeViewApp::AddToolbar( boost::filesystem::path& i_RootPath ) {

    boost::filesystem::path currentPath = i_RootPath;

    while( currentPath.has_root_path() ){
        WPushButton* currentButton = new WPushButton( "myfile.txt" );

        currentButton->clicked().connect( std::bind([=] () {
            i_RootPath = currentPath;
        }) );
        currentPath = currentPath.parent_path();
    }
}
1

There are 1 answers

0
Apples On

In a lambda function, variables that are captured by value with [=] are const. You seem to be capturing i_RootPath (and everything else) by value, thus it is const.

Judging by your code, you should probably use the capture specification [=,&i_RootPath] to capture only i_RootPath by reference.

You could also use [&] to capture everything by reference, but it looks like you need to keep a constant copy of currentPath. In that case, [&,currentPath] would also work.