【问题标题】:No viable overloaded '=' in std::bind functionstd::bind 函数中没有可行的重载“=”
【发布时间】:2015-06-10 20:19:20
【问题描述】:

如果您单击相应的按钮,我正在尝试将引用变量 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();
    }
}

【问题讨论】:

  • 绑定看起来完全多余

标签: c++ boost boost-filesystem wt


【解决方案1】:

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

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

您也可以使用[&] 通过引用捕获所有内容,但您似乎需要保留currentPath 的恒定副本。在这种情况下,[&,currentPath] 也可以使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-15
    • 2014-12-08
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 2011-05-08
    相关资源
    最近更新 更多