PDA

View Full Version : QVector array declear



zhxys
1st February 2011, 13:11
Is it correct to declear a vector like below? In document, it says ok, but when compiling it, it gives error. If I remove (200), then compiling ok.

QVector<QString> vector(200);

high_flyer
1st February 2011, 13:14
Edit:
Wrong stuff said.

zhxys
1st February 2011, 13:22
thanks for the reply, but both of your examples compiling failed also. (declaration syntax error)

funny enough, this is copied from qt doc
http://doc.qt.nokia.com/latest/qvector.html#details

any ideas?

high_flyer
1st February 2011, 13:36
You original code is correct.
Post you code, the problem is probably somewhere else.

zhxys
2nd February 2011, 07:06
if you copy that line to any header file, and then compile, you will see the compiling error!

ChrisW67
2nd February 2011, 08:30
This:


#include <QString>
#include <QVector>

QVector<QString> vector(200);

int main(int argc, char **argv)
{
}

is just fine for me.

Exactly what is the error message when you compile that?

zhxys
2nd February 2011, 08:42
Hi,
As I said, put it to a class header file for example, then it will give "declaration syntax error". What I want is to define a fix sized vector contains constant strings or other struct.

#include <QString>
#include <QVector>

class MyClass
{
...

private:
QVector<QString> vector(200);
};

Thanks,

high_flyer
2nd February 2011, 09:01
Ah, of course, you can't intialize like that in a header!
In you header you do:

QVector<QString> vector;
And in the implementation then you do:

vector.resize(200);

ChrisW67
2nd February 2011, 09:17
Or, a slight variation using the initialisation list:


// test.h
class Test
{
QVector<QString> vector;
public:
Test();
};

// test.cpp
Test::Test():
vector(200)
{
}