for (int i = 0; i < agvs.size(); i++) { threads.push_back(std::thread(&MainWindow::moveAgvs, this, i)); } for (auto& t : threads) { t.join(); }, QTimer::singleShot(time, this, &, i, next_node, cur_x, cur_y, power_consumption, j, steps { // 离开当前位置时将标记设为0 float ratio = static_cast<float>(j) / steps; double new_x = cur_x + ratio * (next_node->x - cur_x); double new_y = cur_y + ratio * (next_node->y - cur_y); agvs[i].setCurrentX(new_x); agvs[i].setCurrentY(new_y); // std::cout <<"AGV: "<< i+1 <<"(" << new_x << "," << new_y << ")" << endl; // 更新电量 if (agvs[i].battery_ - power_consumption / steps < 0) { agvs[i].setbattery(0); } else { agvs[i].setbattery(agvs[i].battery_ - power_consumption / steps); } this->update(); // 在窗口中重绘 if (agvs[i].getBattery() >0 && std::floor(new_x) == new_x && std::floor(new_y) == new_y) { // 如果new_x和new_y为整数型,将其转换为浮点型后调用moveAgv()函数 moveAgvs(i); } });報錯:std::thread和Qtime發生衝突,怎麽修改
时间: 2024-03-05 17:53:54 浏览: 59
自动导向车系统AGVS的构成-自动化立体仓库技术及应用
这个错误可能是由于Lambda表达式中的参数和QTimer::singleShot方法的参数不匹配导致的。在Lambda表达式中,参数是使用&符号传递的,而在QTimer::singleShot方法中,参数是使用逗号分隔的。为了解决这个错误,您可以尝试使用以下代码:
```
QTimer::singleShot(time, this, [&, i, next_node, cur_x, cur_y, power_consumption, j, steps]() { // 离开当前位置时将标记设为0
float ratio = static_cast<float>(j) / steps;
double new_x = cur_x + ratio * (next_node->x - cur_x);
double new_y = cur_y + ratio * (next_node->y - cur_y);
agvs[i].setCurrentX(new_x);
agvs[i].setCurrentY(new_y);
// std::cout <<"AGV: "<< i+1 <<"(" << new_x << "," << new_y << ")" << endl;
// 更新电量
if (agvs[i].battery_ - power_consumption / steps < 0) {
agvs[i].setbattery(0);
} else {
agvs[i].setbattery(agvs[i].battery_ - power_consumption / steps);
}
this->update(); // 在窗口中重绘
if (agvs[i].getBattery() >0 && std::floor(new_x) == new_x && std::floor(new_y) == new_y) {
// 如果new_x和new_y为整数型,将其转换为浮点型后调用moveAgv()函数
moveAgvs(i);
}
});
```
这里的修改是将Lambda表达式中的参数列表改为使用引用符号&,并且去掉了QTimer::singleShot方法中的逗号。这样就可以避免std::thread和QTimer发生冲突的问题。
阅读全文