Search code examples
qtqmap

QMap does not name a type


I'm starting out with Qt Creator and I'm working on mapping some key value pairs (AWG wire gauge sizes as keys and the diameter as value). In using QMap to create the map, I'm getting compile errors of 'awg_map does not name a type'. Can anyone point me in the right direction here?

I've tried adding values two ways that are supposed to work, according to the instructions found at http://doc.qt.io/qt-5/qmap.html. Both ways generate the error noted above.

#include <QMap>

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

Solution

  • Looks like G.M. was right with this comment

    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).

    I dropped it all into the MainWindow::MainWindow and it compiled fine. Thanks for that one GM!