-1

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);
Max
  • 95
  • 1
  • 13
  • All four of these methods fail (Yes, I'm aware about duplicate keys, these were tried individually.) awg_map["20"] = 0.812; awg_map.insert("21", 0.723); awg_map[QString::number("20")] = float(0.812); awg_map.insert(QString::number("21"), float(0.723)); – Max Dec 12 '18 at 21:49
  • Here's the Compile Output error: ... 15:48:07: Starting: "/usr/bin/make" g++ -c -m64 -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++-64 -I../winder -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I../winder -I. -o mainwindow.o ../winder/mainwindow.cpp ../winder/mainwindow.cpp:9:1: error: 'awg_map' does not name a type awg_map[QString::number("20")] = float(0.812); ^ ../winder/mainwindow.cpp:10:1: error: 'awg_map' does not name a type awg_map.insert(QString::number("21"), float(0.723)); ^ – Max Dec 12 '18 at 21:51
  • do not use float, it does not exist in C++, change `QString::number("20")` to `QString::number(20)` – eyllanesc Dec 12 '18 at 21:52
  • 1
    Finally, do not put important information as the complete error message in the comments, that should be in your question. I recommend you read [ask] and pass the [tour] again so that you understand that it is a quality question. – eyllanesc Dec 12 '18 at 21:54
  • Respectfully, the only error message IS in the original question "awg_map does not name a type". Type definition of the element keys as either ["20"], [QString::number("20")], or [QString::number(20)] all fail with precisely the same error message. Changing the key->value type from either float, double, or int (using an actual integer) also completely fails. Obviously something is wrong with the QMap type definition, I just don't know what that might be. – Max Dec 12 '18 at 22:03
  • 2
    You're using a very old Qt version (Qt4?!) but that in itself shouldn't be a problem. Is your Qt installation successfully building the example programs available in Creator? At any rate, the code in the OP is okay and works. This is a build / environment problem not a code problem. –  Dec 13 '18 at 10:26
  • 2
    If the code snippet shown really is complete then it doesn't look like valid `c++` to me. 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). My guess is that for `awg_map["20"] = 0.812;` the compiler is trying to parse that as an anonymous array with element type `awg_map` and size `"20"` -- whatever that means. Do you not get any error/warning regarding the use of a non-integral type being used to specify an array size (or similar)? – G.M. Dec 13 '18 at 10:39
  • Thank you replete and GM. I had a feeling that installation might be an issue. Im running on a Linux machine and install seemed like it went ok. Might have to purge and reinstall it and see what happens. GM, I don't get any other error messages. I'll try sticking it inside a function before doing the reinstall and report back first. However, the snippet is written just as it is recommended on the qt.io website. – Max Dec 13 '18 at 11:52
  • Does this version info on About QT Creator look off to anyone? Qt Creator 3.5.1 Based on Qt 5.5.1 (GCC 5.2.1 20151129, 64 bit) – Max Dec 13 '18 at 12:44
  • It is a couple of years old, that's all. The most important thing is to make the correction G.M. mentioned and enclose the code in a function. It's invalid as written, the Qt example is just a snippet and they assume you know where to put it. Just dump it in your project's main() for now, and let us know whether it compiles. –  Dec 13 '18 at 12:50

1 Answers1

0

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!

Max
  • 95
  • 1
  • 13