Results 1 to 12 of 12

Thread: <vector> and new

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: <vector> and new

    The vector will clear itself when it's destructed so an explicit clear() is not necessary (and remember clear() doesn't free the memory occupied by items stored in the vector).

  2. The following user says thank you to wysota for this useful post:

    mickey (18th May 2006)

  3. #2
    Join Date
    Jan 2006
    Posts
    976
    Qt products
    Qt3
    Platforms
    Windows
    Thanks
    53

    Default Re: <vector> and new

    Sorry I've still compile problem; the first compile (and seem works too), the 2th no;Why? What is happening?
    Qt Code:
    1. class Scene {
    2. std::vector<Light> light;
    3. };
    4.  
    5. Scene::Scene() {
    6. light.push_back(Light());
    7. }
    8.  
    9. Scene::~Scene() {
    10. light.clear();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class Scene {
    2. std::vector<Light> light(1); //with (1) don't compile
    3. };
    4.  
    5. Scene::Scene() {
    6. light.push_back(Light());
    7. }
    8.  
    9. Scene::~Scene() {
    10. light.clear();
    11. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. qsignalslotimp.h(66): fatal error C1903: unable to recover from previous error(s); stopping compilation
    2. mainform.ui.h(75): error C2109: subscript requires array or pointer type
    3. mainform.ui.h(75): error C2228: left of '.getPosition' must have class/struct/union type
    4. mainform.ui.h(75): error C2475: 'Scene::light' : forming a pointer-to-member requires explicit use of the address-of operator ('&') and a qualified name
    5. mainform.ui.h(75): error C2475: 'Scene::light' : forming a pointer-to-member requires explicit use of the address-of operator ('&') and a qualified name
    6. mainform.ui.h(959): error C2109: subscript requires array or pointer type
    7. mainform.ui.h(959): error C2228: left of '.setXPosition' must have class/struct/union type
    8. ..........................................
    To copy to clipboard, switch view to plain text mode 
    Last edited by mickey; 18th May 2006 at 14:35.
    Regards

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: <vector> and new

    It should be:
    Qt Code:
    1. class Scene
    2. {
    3. public:
    4. Scene();
    5. // ...
    6. private:
    7. std::vector<Light> light;
    8. };
    9.  
    10. Scene::Scene() : light(1)
    11. {
    12. // ...
    13. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to jacek for this useful post:

    mickey (18th May 2006)

  6. #4
    Join Date
    Jan 2006
    Posts
    976
    Qt products
    Qt3
    Platforms
    Windows
    Thanks
    53

    Default Re: <vector> and new

    OK It works! but I don't understand where's the problem in my solutions.. Can't I give dimension of vector inside class? thanks
    Regards

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: <vector> and new

    Quote Originally Posted by mickey
    Can't I give dimension of vector inside class?
    No, you can't. You can only declare member variables there.

  8. The following user says thank you to jacek for this useful post:

    mickey (18th May 2006)

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
  •  
Qt is a trademark of The Qt Company.