4

I seem to be having an issue using basic vectors in Qt, where I keep getting a compile error. The exact information will be posted below:

Code snippet:

....
#include <QVector>
#include <QString>

QVector<QString> vector;
vector.append("sometext");

Error message:

'vector' does not name a type

This error appears for any piece of code that directly pertains to the created vector, not just the append function. Any insight to what I am doing wrong would be appreciated. The vector exists. I've tested it by initializing all of it's elements using one item and accessing it in other parts of the program.

Mat
  • 202,337
  • 40
  • 393
  • 406
Ruiz
  • 93
  • 2
  • 6

1 Answers1

9
#include <QVector>
#include <QString>

QVector<QString> vector;
vector.append("sometext");

If this is your real code, then you are doing the append outside of any function, which you can't do in c++, and which will cause the exact compilation error you mentioned:

enter image description here enter image description here

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • 1
    This is precisely the case. My professor neglected to express something so simple. I will accept this answer as soon as this website enables me to do so. – Ruiz Nov 26 '13 at 10:03
  • Fix: `QVector vector(1, "sometext");`. If you need more values, you need C++11: `QVector vector { "one", "two", "three" };` – MSalters Nov 26 '13 at 12:37