Results 1 to 9 of 9

Thread: QVector array declear

  1. #1
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded PyQt3 PyQt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default QVector array declear

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

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QVector array declear

    Edit:
    Wrong stuff said.
    Last edited by high_flyer; 1st February 2011 at 13:35.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded PyQt3 PyQt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: QVector array declear

    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QVector array declear

    You original code is correct.
    Post you code, the problem is probably somewhere else.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded PyQt3 PyQt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: QVector array declear

    if you copy that line to any header file, and then compile, you will see the compiling error!

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QVector array declear

    This:
    Qt Code:
    1. #include <QString>
    2. #include <QVector>
    3.  
    4. QVector<QString> vector(200);
    5.  
    6. int main(int argc, char **argv)
    7. {
    8. }
    To copy to clipboard, switch view to plain text mode 
    is just fine for me.

    Exactly what is the error message when you compile that?

  7. #7
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded PyQt3 PyQt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: QVector array declear

    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,
    Last edited by zhxys; 2nd February 2011 at 08:47.

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QVector array declear

    Ah, of course, you can't intialize like that in a header!
    In you header you do:
    Qt Code:
    1. QVector<QString> vector;
    To copy to clipboard, switch view to plain text mode 
    And in the implementation then you do:
    Qt Code:
    1. vector.resize(200);
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. The following user says thank you to high_flyer for this useful post:

    zhxys (3rd February 2011)

  10. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QVector array declear

    Or, a slight variation using the initialisation list:
    Qt Code:
    1. // test.h
    2. class Test
    3. {
    4. QVector<QString> vector;
    5. public:
    6. Test();
    7. };
    8.  
    9. // test.cpp
    10. Test::Test():
    11. vector(200)
    12. {
    13. }
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to ChrisW67 for this useful post:

    zhxys (3rd February 2011)

Similar Threads

  1. Replies: 2
    Last Post: 12th November 2010, 14:42
  2. Replies: 1
    Last Post: 4th August 2010, 17:13
  3. declare an array of QSemaphore and array of slot functions
    By radeberger in forum Qt Programming
    Replies: 11
    Last Post: 2nd May 2010, 13:24
  4. use QVector as 2 dimensional Array
    By umulingu in forum Qt Programming
    Replies: 3
    Last Post: 1st January 2010, 12:31
  5. QVector crashes when array size is big
    By Sheng in forum Qt Programming
    Replies: 49
    Last Post: 27th February 2009, 22:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.