如果您单击相应的按钮,我会尝试将引用变量 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/