Results 1 to 14 of 14

Thread: vector with own class

  1. #1
    Join Date
    Nov 2009
    Location
    Austria
    Posts
    30
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Question vector with own class

    hi people,

    i got some troubles with a vector. i need an array of steps( <- i created my own step-class) for loading a stack, because i don't know how many there will be i thought of using a vector. but i actually can't create one and i don't know why.

    here my step class:
    Qt Code:
    1. class step {
    2.  
    3. public:
    4. step();
    5. step(int a, int b);
    6.  
    7. int id_sm;
    8. int id_s;
    9. int id_src;
    10.  
    11. int type;
    12.  
    13. int dest_vp_x;
    14. int dest_vp_y;
    15. int dest_vp_z;
    16. int dest_vp_dreh;
    17. int dest_x;
    18. int dest_y;
    19. int dest_z;
    20. int dest_dreh;
    21.  
    22.  
    23. };
    To copy to clipboard, switch view to plain text mode 

    and i tried this one:
    Qt Code:
    1. QVector<step> steps;
    2. for(int i = 0; i < step_count; i++){
    3.  
    4. steps.append(new step(42,i));
    5. }
    To copy to clipboard, switch view to plain text mode 
    but he says no matching function for this call?

    anyone knows why or knows an idea what else i could use?

    thx
    tobi

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: vector with own class

    Quote Originally Posted by T0bi4s View Post
    and i tried this one:
    Qt Code:
    1. QVector<step> steps;
    To copy to clipboard, switch view to plain text mode 
    here you say that vector stores step objects
    Qt Code:
    1. for(int i = 0; i < step_count; i++){
    2.  
    3. steps.append(new step(42,i));
    4. }
    To copy to clipboard, switch view to plain text mode 
    and here you want to append pointer to an step object so it won't work for sure, it is C++ not Java :P
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. #3
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: vector with own class

    Quote Originally Posted by T0bi4s View Post
    Qt Code:
    1. QVector<step> steps;
    2. for(int i = 0; i < step_count; i++) {
    3. steps.append(new step(42,i));
    4. }
    To copy to clipboard, switch view to plain text mode 
    Try storing pointers in the vector like this:
    Qt Code:
    1. QVector<step*> steps;
    2. for(int i = 0; i < step_count; i++) {
    3. steps.append(new step(42,i));
    4. }
    To copy to clipboard, switch view to plain text mode 

    I always create a typedef for the templated classes in my code. If you like, you can create a typedef for your step vector like so:

    Qt Code:
    1. typedef QVector<step*> StepPtrVector;
    2. StepPtrVector steps;
    To copy to clipboard, switch view to plain text mode 
    Hope that helps.

    jthomps
    Last edited by jefftee; 9th January 2010 at 07:03.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: vector with own class

    But why store pointers if the step class is perfectly value-based? Unless of course you like your programs to leak memory or you like to have more work than required...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: vector with own class

    Quote Originally Posted by wysota View Post
    But why store pointers if the step class is perfectly value-based? Unless of course you like your programs to leak memory or you like to have more work than required...
    Check his original post, he's doing a "new step", which is why QVector<step> didn't work...

    I was simply pointing out that he was doing a "new step" in line 3, which requires that the QVector template be QVector<step*>.He can store step values, references, or pointers, but the QVector declaration needs to match.

    Not sure what you meant by pointers leaking memory. Pointers don't leak memory, bad programmers who don't know how to clean up pointers do...

    jthomps

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: vector with own class

    Quote Originally Posted by jthomps View Post
    Check his original post, he's doing a "new step", which is why QVector<step> didn't work...

    I was simply pointing out that he was doing a "new step" in line 3, which requires that the QVector template be QVector<step*>.He can store step values, references, or pointers, but the QVector declaration needs to match.
    So he should not be doing "new step" but step(). Don't encourage him to do wrong things.

    Not sure what you meant by pointers leaking memory. Pointers don't leak memory, bad programmers who don't know how to clean up pointers do...
    I mean you have to clear the vector manually after you're done with it (hence "more work") which is not required when you use value-based vectors.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: vector with own class

    Quote Originally Posted by jthomps View Post
    He can store step references ...
    Can he? Like this:
    Qt Code:
    1. QList<step &> steps;
    To copy to clipboard, switch view to plain text mode 
    ???
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  8. #8
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default

    Quote Originally Posted by wysota View Post
    So he should not be doing "new step" but step(). Don't encourage him to do wrong things.
    That sums it up right there. You consider using pointers bad and the rest of the C++ world doesn't...

    Quote Originally Posted by faldżip View Post
    Can he? Like this:
    Qt Code:
    1. QList<step &> steps;
    To copy to clipboard, switch view to plain text mode 
    Oops, my bad, scratch the by reference, which leaves by value or by pointer...

    Regards,

    jthomps
    Last edited by wysota; 10th January 2010 at 15:36.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: vector with own class

    Quote Originally Posted by jthomps View Post
    That sums it up right there. You consider using pointers bad and the rest of the C++ world doesn't...
    No, I don't. But you should know when to use pointers and when not to use them. Which of the following would you prefer and why?

    Qt Code:
    1. void func1() {
    2. int x;
    3. doSomething(x);
    4. }
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. void func2() {
    2. int *x = new int;
    3. doSomething(*x);
    4. delete x;
    5. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: vector with own class

    Normally I use pointers with vectors simply so that if I need to pass an item somewhere I can simply pass a pointer instead so avoid the object being copied for the parameter. However, starting with Qt, I find it easier to use implicitly shared classes, so the data is shared between the object unless it is modified (copy on write strategy).

  11. #11
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: vector with own class

    Of course I'd prefer the first version of the function... There's no benefit to passing around pointers to built in types, but if that were a QByteArray or one of my own classes that is fairly large instead of a int, I'd probably use a new/delete and manage the pointers.

    BTW, I just noticed that you're the owner (or admin) for Qt Centre site. Great site and love the recent upgrade. Keep up the good work!

    Regards,

    jthomps

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: vector with own class

    Quote Originally Posted by jthomps View Post
    There's no benefit to passing around pointers to built in types, but if that were a QByteArray or one of my own classes that is fairly large instead of a int,
    What's the benefit of passing QByteArray using a pointer compared to an int?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Nov 2009
    Location
    Austria
    Posts
    30
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: vector with own class

    hi,

    it's me again i still got trouble with this vector, and i don't really get your point with what you mean with value-based?
    i mean yes the class step is value based, so shouldn't i use pointers? or what you want to tell me?

    my c and c++ is quite bad cause i just used java the last 2 years so i may need some generell information about, saving, in this case, steps. :/

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: vector with own class

    Quote Originally Posted by T0bi4s View Post
    it's me again i still got trouble with this vector, and i don't really get your point with what you mean with value-based?
    I mean it contains only POD values and no pointers, so you can safely copy it around.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 3
    Last Post: 27th December 2008, 19:34
  2. vector of vector and computes
    By mickey in forum General Programming
    Replies: 1
    Last Post: 15th May 2008, 12:47
  3. Replies: 3
    Last Post: 16th May 2007, 11:07
  4. insert in a vector of vector
    By mickey in forum General Programming
    Replies: 3
    Last Post: 6th March 2007, 08:45
  5. vector of vector size
    By mickey in forum General Programming
    Replies: 5
    Last Post: 13th February 2007, 15:59

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.