没有可行的超载' ='在std :: bind函数中

时间:2015-06-10 20:19:21

标签: c++ boost boost-filesystem wt

如果您单击相应的按钮,我试图将引用变量i_RootPath的值设置为while循环内的不同值。编译不喜欢我分配i_RootPath的方式。它说:

  

没有可行的超载' ='

如何成功更改" i_RootPath"的值?从生成的按钮调用的不同方法中?

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 个答案:

答案 0 :(得分:5)

在lambda函数中,使用[=]的值捕获的变量为const。您似乎按值捕获i_RootPath(以及其他所有内容),因此它是const

根据您的代码判断,您应该使用捕获规范[=,&i_RootPath]仅通过引用捕获i_RootPath

您也可以使用[&]通过引用捕获所有内容,但看起来您需要保留currentPath的常量副本。在这种情况下,[&,currentPath]也可以。

相关问题