void MainWindow::moveAgvs() { Astar astar; std::vector<std::vector<Node*>> paths(agvs.size()); //根據agv獲取taskid,初始化 int completed_task_index = -1; // 如果任務都完成了,停止定時器 bool all_tasks_completed = true; for (int j = 0; j < tasks.size(); j++) { if (tasks[j].completed != 2) { all_tasks_completed = false; break; } } if (all_tasks_completed) { timer->stop(); // 停止定时器 return; } // 得到agv的路綫 for (int i = 0; i < agvs.size(); i++) { if (agvs[i].getLoad() == true) { // 如果是负载的状态 if (agvs[i].getCurrentX() == agvs[i].getEndX() && agvs[i].getCurrentY() == agvs[i].getEndY()) { // 如果到达终点 agvs[i].setLoad(false); // 设置为空载状态 agvs[i].setState(true); std::cout << "agv__id :" << agvs[i].getid() << " ,agv_get_task_id :" << agvs[i].get_task_id() << endl; for (int j = 0; j < tasks.size(); j++) { if (tasks[j].id == agvs[i].get_task_id()) { completed_task_index = j; break; } } if (completed_task_index != -1) { tasks[completed_task_index].completed = 2; } task_to_agv(); // 更新任务分配 update(); // 更新AGV状态 } else { // 否则行驶到终点 Node* start_node = new Node(agvs[i].getCurrentX(), agvs[i].getCurrentY()); Node* end_node1 = new Node(agvs[i].getEndX(), agvs[i].getEndY()); std::vector<Node*> path = astar.getPath(start_node, end_node1); path.erase(path.begin()); paths[i] = path; } } else { // 如果是空载的状态 if (agvs[i].getCurrentX() == agvs[i].getStartX() && agvs[i].getCurrentY() == agvs[i].getStartY()) { // 如果到达起点 agvs[i].setLoad(true); // 设置为负载状态 } else { // 否则行驶到起点 Node* start_node = new Node(agvs[i].getCurrentX(), agvs[i].getCurrentY()); Node* end_node = new Node(agvs[i].getStartX(), agvs[i].getStartY()); std::vector<Node*> path = astar.getPath(start_node, end_node); path.erase(path.begin()); paths[i] = path; } } },怎麽將path變爲成員變量,而不是局部變量
时间: 2024-01-08 08:04:02 浏览: 149
Qt5主窗体程序:文本编辑器的实现(Qt5开发及实例)
要将 `path` 变成成员变量,需要将 `paths` 定义为一个存储 `std::vector<Node*>` 的 vector,即 `std::vector<std::vector<Node*>> paths` 改为 `std::vector<std::vector<Node*>> paths_;`,然后在 `moveAgvs()` 函数中将 `path` 赋值给 `paths_` 的相应位置即可。
修改后的代码如下:
```
void MainWindow::moveAgvs() {
Astar astar;
std::vector<std::vector<Node*>> paths(agvs.size());
//根據agv獲取taskid,初始化
int completed_task_index = -1;
// 如果任務都完成了,停止定時器
bool all_tasks_completed = true;
for (int j = 0; j < tasks.size(); j++) {
if (tasks[j].completed != 2) {
all_tasks_completed = false;
break;
}
}
if (all_tasks_completed) {
timer->stop(); // 停止定时器
return;
}
// 得到agv的路綫
for (int i = 0; i < agvs.size(); i++) {
if (agvs[i].getLoad() == true) { // 如果是负载的状态
if (agvs[i].getCurrentX() == agvs[i].getEndX() && agvs[i].getCurrentY() == agvs[i].getEndY()) { // 如果到达终点
agvs[i].setLoad(false); // 设置为空载状态
agvs[i].setState(true);
std::cout << "agv__id :" << agvs[i].getid() << " ,agv_get_task_id :" << agvs[i].get_task_id() << endl;
for (int j = 0; j < tasks.size(); j++) {
if (tasks[j].id == agvs[i].get_task_id()) {
completed_task_index = j;
break;
}
}
if (completed_task_index != -1) {
tasks[completed_task_index].completed = 2;
}
task_to_agv(); // 更新任务分配
update(); // 更新AGV状态
} else { // 否则行驶到终点
Node* start_node = new Node(agvs[i].getCurrentX(), agvs[i].getCurrentY());
Node* end_node1 = new Node(agvs[i].getEndX(), agvs[i].getEndY());
std::vector<Node*> path = astar.getPath(start_node, end_node1);
path.erase(path.begin());
paths_[i] = path;
}
} else { // 如果是空载的状态
if (agvs[i].getCurrentX() == agvs[i].getStartX() && agvs[i].getCurrentY() == agvs[i].getStartY()) { // 如果到达起点
agvs[i].setLoad(true); // 设置为负载状态
} else { // 否则行驶到起点
Node* start_node = new Node(agvs[i].getCurrentX(), agvs[i].getCurrentY());
Node* end_node = new Node(agvs[i].getStartX(), agvs[i].getStartY());
std::vector<Node*> path = astar.getPath(start_node, end_node);
path.erase(path.begin());
paths_[i] = path;
}
}
}
}
```
阅读全文