Results 1 to 8 of 8

Thread: Pointer to 2D Vector

  1. #1
    Join Date
    Feb 2014
    Posts
    23
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Pointer to 2D Vector

    Hi All!

    I have created several 2D vectors, all operating in the same fashion, and a hopeful pointer for them

    Qt Code:
    1. QVector< QVector<MapSpace> > p1map;
    2. QVector< QVector<MapSpace> > p2map;
    3. QVector< QVector<MapSpace> > p3map;
    4. QVector< QVector<MapSpace> > p4map;
    5. QVector< QVector<MapSpace> >* pathfix;
    To copy to clipboard, switch view to plain text mode 

    I have a function which given particular cases, will modify one of the p1-p4maps. Rather than having to create different instances for each particular map I'm modifying, I was hoping to point pathfix to the map needing updating, allowing the one instance of the function to modify the desired map. However, I cannot seem to figure out how to declare and assign the pointer in a manner that will grant access to the public functions of a MapSpace. I can take out the * before pathfix and do

    Qt Code:
    1. pathfix = p1map;
    To copy to clipboard, switch view to plain text mode 

    But this does not actually change p1map and results in a lot of extra operations in copying the entire matrix. I could later assign p1map the value of pathfix, but this would result in double the matrix copy operations. All of this seems easily avoidable if I can point to the entire 2D vector.

    Note: I was hoping to be able to access using [][] notation on my pointer.

    Thanks!


    Added after 12 minutes:


    But this does not actually change p1map and results in a lot of extra operations in copying the entire matrix. I could later assign p1map the value of pathfix, but this would result in double the matrix copy operations. All of this seems easily avoidable if I can point to the entire 2D vector.
    After walking away for a moment, this statement may not be entirely correct, I'm still debugging a lot of my recent changes so I haven't been able to see what happens.

    Directly assigning one vector to another might be the solution, but it depends on if the variable is actually a pointer as in C++ arrays.
    Last edited by squeegedog; 5th March 2014 at 11:03.

  2. #2
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    29
    Thanked 50 Times in 47 Posts

    Default Re: Pointer to 2D Vector

    Do like this:-
    Qt Code:
    1. pathfix = &p1map;
    To copy to clipboard, switch view to plain text mode 
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

  3. #3
    Join Date
    Feb 2014
    Posts
    23
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Pointer to 2D Vector

    Quote Originally Posted by sonulohani View Post
    Do like this:-
    Qt Code:
    1. pathfix = &p1map;
    To copy to clipboard, switch view to plain text mode 
    I should have noted some of the things I've tried to this point.

    Qt Code:
    1. QVector< QVector<MapSpace> > pathfix;
    To copy to clipboard, switch view to plain text mode 
    Returns error:
    error: no match for 'operator=' in 'pathfix = &((GameScene*)this)->GameScene:1map'

    Qt Code:
    1. QVector< QVector<MapSpace> >* pathfix;
    To copy to clipboard, switch view to plain text mode 
    error: 'class QVector<MapSpace>' has no member named 'ispassable'

    Qt Code:
    1. QVector< QVector<MapSpace> >** pathfix;
    To copy to clipboard, switch view to plain text mode 
    cannot convert 'QVector<QVector<MapSpace> >*' to 'QVector<QVector<MapSpace> >**' in assignment

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

    Default Re: Pointer to 2D Vector

    None of the lines and errors you show match the declarations you show. The errors are associated with assignments or some undisclosed call to ispassable(). We cannot fix what we cannot see.

    In any case, since the vector must exist why not just pass the QVector by reference (const or not)?

    Qt Code:
    1. #include <QtCore>
    2.  
    3. struct MapSpace { };
    4.  
    5. void someFunc(QVector< QVector<MapSpace> >* v) {
    6. qDebug() << Q_FUNC_INFO << v->size();
    7. v->append(QVector<MapSpace>());
    8. }
    9.  
    10. void someOtherFunc(QVector< QVector<MapSpace> > &v) {
    11. qDebug() << Q_FUNC_INFO << v.size();
    12. v.append(QVector<MapSpace>());
    13. }
    14.  
    15.  
    16. int main(int argc, char **argv) {
    17. QCoreApplication app(argc, argv);
    18.  
    19. QVector< QVector<MapSpace> > p1map(10); // different initial sizes for demonstration
    20. QVector< QVector<MapSpace> > p2map(5);
    21. QVector< QVector<MapSpace> > *pathfix = 0;
    22.  
    23. pathfix = &p1map; // no error
    24. someFunc(pathfix); // no error here either
    25. pathfix = &p2map;
    26. someFunc(pathfix);
    27.  
    28. // Save yourself some typing
    29. someFunc(&p1map);
    30. someFunc(&p2map);
    31.  
    32. // Or pass by reference
    33. someOtherFunc(p1map);
    34. someOtherFunc(p2map);
    35.  
    36. return 0;
    37. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2014
    Posts
    23
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Pointer to 2D Vector

    Quote Originally Posted by ChrisW67 View Post
    None of the lines and errors you show match the declarations you show. The errors are associated with assignments or some undisclosed call to ispassable(). We cannot fix what we cannot see.
    Sorry, I thought it was clear that I was showing the different errors received when I ran the line
    Qt Code:
    1. pathfix = &p1map
    To copy to clipboard, switch view to plain text mode 
    Based on how I declared the variable pathfix. The errors with ispassable() was showing that I could not access the public member functions of the Mapspace when instantiating the pointer in that manner. All of the code being ran operated flawlessly before I realized I had a need to create more than one 2d vector.

    The problem I'm facing with using your proposed solution is that this methodology of determining which map to use will be used in several functions, and most of them are being called by a class that is not aware of the 2d vectors

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

    Default Re: Pointer to 2D Vector

    Quote Originally Posted by squeegedog View Post
    Sorry, I thought it was clear that I was showing the different errors received when I ran the line
    Qt Code:
    1. pathfix = &p1map
    To copy to clipboard, switch view to plain text mode 
    Based on how I declared the variable pathfix.
    Qt Code:
    1. // Fails because you are assigning a pointer to a non-pointer variable.
    2. QVector< QVector<MapSpace> > pathfix;
    3. pathfix = &p1map;
    4.  
    5. // Does not fail. Your error message is from unrelated code.
    6. QVector< QVector<MapSpace> >* pathfix;
    7. pathfix = &p1map;
    8.  
    9. // Fails because a pointer-to-QVector<...> is not a pointer-to-pointer-to-QVector<...>
    10. QVector< QVector<MapSpace> >** pathfix;
    11. pathfix = &p1map;
    To copy to clipboard, switch view to plain text mode 

    The errors with ispassable() was showing that I could not access the public member functions of the Mapspace when instantiating the pointer in that manner.
    This is one way you would access an MapSpace function given a pointer to the nested vectors:
    Qt Code:
    1. #include <QtCore>
    2.  
    3. struct MapSpace {
    4. void func() { qDebug() << Q_FUNC_INFO;}
    5. void constFunc() const { qDebug() << Q_FUNC_INFO; }
    6. };
    7.  
    8. void someFunc(QVector< QVector<MapSpace> >* v) {
    9. qDebug() << Q_FUNC_INFO;
    10.  
    11. for (int i = 0; i < v->size(); ++i) { // outer vector
    12. for (int j = 0; j < v->at(i).size(); ++j) { // inner vector
    13. qDebug() << i << j;
    14. // +- returns const reference to a QVector<MapSpace>
    15. // | + returns const reference to a MapSpace
    16. // | | + on which we call
    17. // V V V
    18. v->at(i).at(j).constFunc();
    19.  
    20. // +- returns non const reference to a QVector<MapSpace>
    21. // | + returns non-const reference to a MapSpace
    22. // | | + on which we call
    23. // V V V
    24. v->operator[](i).operator[](j).func();
    25. }
    26. }
    27. }
    28.  
    29. int main(int argc, char **argv) {
    30. QCoreApplication app(argc, argv);
    31. QVector< QVector<MapSpace> > p1map;
    32. p1map << QVector<MapSpace>(2);
    33. p1map << QVector<MapSpace>(3);
    34. someFunc(&p1map);
    35. return 0;
    36. }
    To copy to clipboard, switch view to plain text mode 

    The problem I'm facing with using your proposed solution is that this methodology of determining which map to use will be used in several functions, and most of them are being called by a class that is not aware of the 2d vectors
    Not sure what you mean.

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

    squeegedog (6th March 2014)

  8. #7
    Join Date
    Feb 2014
    Posts
    23
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Pointer to 2D Vector

    I found what was making everything not work using it as a pointer.

    Qt Code:
    1. QVector< QVector<MapSpace> > p1map;
    2. QVector< QVector<MapSpace> > p2map;
    3. QVector< QVector<MapSpace> > p3map;
    4. QVector< QVector<MapSpace> > p4map;
    5. QVector< QVector<MapSpace> >* pathfix;
    6.  
    7. pathfix = &p1map
    8.  
    9. pathfix[x][y].ispassable(); //this was not working
    10. (*pathfix)[x][y].ispassable(); //works like a charm
    To copy to clipboard, switch view to plain text mode 

    Thanks for all your help.

    Not sure what you mean.
    And I meant that in many of the functions that call to operate on the MapSpace Vector, are not aware of the Vector, so passing the Vector in as a parameter would be impossible. I was trying to keep my question as simple as possible without showing hundreds of lines of code, and somewhat over-simplified the code I gave, possibly leading to why noone discovered I was missing the (*) around my pointer variable, but all is well now.

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

    Default Re: Pointer to 2D Vector

    Indeed, that is a neater construct.

Similar Threads

  1. Copying vector of pointers to another vector
    By Momergil in forum Newbie
    Replies: 12
    Last Post: 24th September 2011, 23:09
  2. vector of vector and computes
    By mickey in forum General Programming
    Replies: 1
    Last Post: 15th May 2008, 13:47
  3. vector iterator as pointer problem
    By Teerayoot in forum General Programming
    Replies: 3
    Last Post: 6th May 2007, 21:36
  4. insert in a vector of vector
    By mickey in forum General Programming
    Replies: 3
    Last Post: 6th March 2007, 09:45
  5. vector of vector size
    By mickey in forum General Programming
    Replies: 5
    Last Post: 13th February 2007, 16: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.