foreach (QString str, list) { qDebug() << str; const QString studentID = str; for(auto& studentID : students){ auto studentIt = m_students.find(studentID); if(studentIt == m_students.end()){ qDebug() << "student is not exist"; return; } auto it = studentIt.value()->m_workMap.find(actName); if(it != studentIt.value()->m_workMap.end()){ qDebug() << "activity repeat"; return; } activity* act = new activity; act->m_name = actName; act->m_startTime = time; act->m_endTime = -1; act->m_type = type; act->m_data.push_back(data); act->m_week.push_back(week); studentIt.value()->m_workMap.insert(act->m_name, act); log(m_currentPeople, "发布了活动[" + actName + "]给[" + studentIt.value()->m_name + "]"); } }
时间: 2024-02-10 18:08:32 浏览: 80
这段代码中使用了两个名为`studentID`的变量,一个是foreach语句中的迭代变量,另一个是for循环中的auto& studentID。这会导致编译错误,因为在同一作用域中不能有同名变量。如果你想在for循环中使用迭代变量,可以将其更改为不同的名称,例如:
```
foreach (QString str, list) {
qDebug() << str;
const QString studentID = str;
for(auto& id : students){
auto studentIt = m_students.find(id);
if(studentIt == m_students.end()){
qDebug() << "student is not exist";
return;
}
auto it = studentIt.value()->m_workMap.find(actName);
if(it != studentIt.value()->m_workMap.end()){
qDebug() << "activity repeat";
return;
}
activity* act = new activity;
act->m_name = actName;
act->m_startTime = time;
act->m_endTime = -1;
act->m_type = type;
act->m_data.push_back(data);
act->m_week.push_back(week);
studentIt.value()->m_workMap.insert(act->m_name, act);
log(m_currentPeople, "发布了活动[" + actName + "]给[" + studentIt.value()->m_name + "]");
}
}
```
这样就能避免同名变量的问题了。
阅读全文