c++ - std::bind 函数中没有可行的重载 '='

标签 c++ boost boost-filesystem wt

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

No viable overloaded '='

如何从生成的按钮调用的不同方法中成功更改“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();
    }
}

最佳答案

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

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

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

关于c++ - std::bind 函数中没有可行的重载 '=',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30766515/

相关文章:

c++ - 哪些编译器支持 std::filesystem?

c++ - 使用静态库的 undefined reference

c++ - ANN OPENCV 错误断言失败

c++ - 避免并发访问变量

c++ - 我究竟该如何使用 Boost?

c++ - 为什么在 C++ 中使用 boost multi_array 索引类型来索引 boost 数组?

c++ - 打印所有继承的类成员变量和方法

c++ - 跨平台MultiByteToWideChar

c++ - 为什么 boost::filesystem 中止而不是抛出异常?

c++ - 在不存在的目录中创建文件的最佳方法