private: Ui::MainWindow ui; std::vector<std::vector<Node>> 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; } } },是否創建了兩個path怎麽修改
时间: 2024-01-20 09:03:58 浏览: 79
是的,代码中定义了一个类成员变量 `std::vector<std::vector<Node>> paths`,并且在 `MainWindow::moveAgvs()` 函数中又定义了一个同名的局部变量 `std::vector<std::vector<Node*>> paths(agvs.size())`,这是重复定义了。
为了避免这种问题,可以将局部变量改名,例如将其改为 `std::vector<std::vector<Node*>> agv_paths(agvs.size())`。
修改后的代码如下:
```
private:
Ui::MainWindow ui;
std::vector<std::vector<Node>> paths;
void MainWindow::moveAgvs() {
Astar astar;
std::vector<std::vector<Node*>> agv_paths(agvs.size()); // 将变量名改为 agv_paths
//根據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());
agv_paths[i] = path; // 将路径保存到 agv_paths 中
}
} 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());
agv_paths[i] = path; // 将路径保存到 agv_paths 中
}
}
}
// 将 agv_paths 赋值给类成员变量 paths
paths = std::vector<std::vector<Node>>(agv_paths.begin(), agv_paths.end());
}
```
阅读全文