使用 QMap 并获取编译错误消息
Using QMap and getting compile error messages
我正在尝试在 class 原型中使用 "QMap":
QMap<QString, TargetsInfo*> m_targets;
在访问方法中我有:
TargetsInfo* TargetsModel::addTarget(int division, int camera, int map) const {
TargetsInfo* target = getTarget(division, camera, map);
if (target == nullptr) {
const QString key = makeKey(division, camera, map);
target = new TargetsInfo();
if ( target != nullptr ) {
m_targets.insert(key, target);
}
}
return target;
}
TargetsInfo* TargetsModel::getTarget(int division, int camera, int map) const {
const QString key = makeKey(division, camera, map);
QMap<QString, TargetsInfo*>::iterator itr = m_targets.find(key);
return (its == m_targets.end()) ? nullptr : itr.value();
}
当我编译时出现以下错误,我正在努力找出问题所在:
C2663: 'QMap<QString, TargetInfo *>::insert': 2 overloads have no legal conversion for 'this' pointer
C2440: 'initialising': cannot convert from 'QMap<QString, TargetInfo *>::const_iterator' to 'QMap::<QString, TargetInfo *>::iterator'
No constructor could take the source type, or constructor overload resolution was ambiguous
为第二次错误修改
QMap<QString, TargetsInfo*>::iterator itr = ...
到
QMap<QString, TargetsInfo*>::const_iterator itr = ...
您的 getTarget
方法是 const,因此 m_targets.find
returns 是一个 const 迭代器(否则您可以在您所谓的 const 方法中更改 m_targets
)。
对于第一个错误,请从您的 addTarget
方法中删除 const
。显然,将项目添加到 m_targets
的方法不应该是 const.
我正在尝试在 class 原型中使用 "QMap":
QMap<QString, TargetsInfo*> m_targets;
在访问方法中我有:
TargetsInfo* TargetsModel::addTarget(int division, int camera, int map) const {
TargetsInfo* target = getTarget(division, camera, map);
if (target == nullptr) {
const QString key = makeKey(division, camera, map);
target = new TargetsInfo();
if ( target != nullptr ) {
m_targets.insert(key, target);
}
}
return target;
}
TargetsInfo* TargetsModel::getTarget(int division, int camera, int map) const {
const QString key = makeKey(division, camera, map);
QMap<QString, TargetsInfo*>::iterator itr = m_targets.find(key);
return (its == m_targets.end()) ? nullptr : itr.value();
}
当我编译时出现以下错误,我正在努力找出问题所在:
C2663: 'QMap<QString, TargetInfo *>::insert': 2 overloads have no legal conversion for 'this' pointer
C2440: 'initialising': cannot convert from 'QMap<QString, TargetInfo *>::const_iterator' to 'QMap::<QString, TargetInfo *>::iterator'
No constructor could take the source type, or constructor overload resolution was ambiguous
为第二次错误修改
QMap<QString, TargetsInfo*>::iterator itr = ...
到
QMap<QString, TargetsInfo*>::const_iterator itr = ...
您的 getTarget
方法是 const,因此 m_targets.find
returns 是一个 const 迭代器(否则您可以在您所谓的 const 方法中更改 m_targets
)。
对于第一个错误,请从您的 addTarget
方法中删除 const
。显然,将项目添加到 m_targets
的方法不应该是 const.