QMap 不命名类型

QMap does not name a type

我从 Qt Creator 着手,正在映射一些键值对(AWG 线规尺寸作为键,直径作为值)。在使用 QMap 创建地图时,我遇到 'awg_map does not name a type' 的编译错误。谁能在这里指出正确的方向?

根据 http://doc.qt.io/qt-5/qmap.html 中的说明,我已尝试以两种应该有效的方式添加值。这两种方式都会产生上述错误。

#include <QMap>

QMap<QString, float> awg_map;
awg_map["20"] = 0.812;
awg_map.insert("21", 0.723);

看起来像 G.M。这个评论是对的

Statements such as awg_map["20"] = 0.812; and awg_map.insert("21", 0.723); aren't valid at global scope and should be in a function body (for example).

我把它全部放到 MainWindow::MainWindow 中,它编译得很好。感谢这位 GM!