Results 1 to 17 of 17

Thread: How to get a pointer for 2D array of QVector?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get a pointer for 2D array of QVector?

    Quote Originally Posted by tfmp View Post
    I was a little confused, and now understand what your codes mean.

    A basic question: If an array is created with FortranMatrix as follows
    Qt Code:
    1. void cppFN()
    2. {
    3. FortranMatrix < int, 3, 5 > intArray;
    4.  
    5. intArray(1,2) =1;
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 
    a user does not need to delete the intArray. It will be cleared by OS when leaving the cppFN(). Is my understanding correct?
    Quote Originally Posted by d_stranz View Post
    Yes, correct.
    but remember that in cpp code, you should be using 'at' to access the array elements, e.g
    Qt Code:
    1. void cppFN()
    2. {
    3. FortranMatrix < int, 3, 5 > intArray;
    4.  
    5. intArray.at(1,2) = 1; // not 'intArray(1,2)' <-- fortran-only syntax
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,329
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to get a pointer for 2D array of QVector?

    but remember that in cpp code, you should be using 'at' to access the array elements, e.g
    No, I think you woke up too early (or maybe are staying awake much too late).

    The code refers to your FortranMatrix template class, which has template methods for operator()( int, int ). His code is exactly correct for that class - the internal storage for that class is a C++ array allocated by operator new(), and the template class doe not define an "at()" method.

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get a pointer for 2D array of QVector?

    Quote Originally Posted by d_stranz View Post
    No, I think you woke up too early (or maybe are staying awake much too late).

    The code refers to your FortranMatrix template class, which has template methods for operator()( int, int ). His code is exactly correct for that class - the internal storage for that class is a C++ array allocated by operator new(), and the template class doe not define an "at()" method.
    I think you are confused.

    I know it refers to my FortranMatrix, which has operator()(...) ONLY for fortran - it is commented as such. Lets remind ourselves:
    Quote Originally Posted by amleto View Post
    I'd be interested in how well this works:
    Qt Code:
    1. template<class T, unsigned int M, unsigned int N>
    2. class FortranMatrix
    3. {
    4. public:
    5. FortranMatrix()
    6. :array_(new T[M][N])
    7. {}
    8.  
    9. // cpp interface
    10. T& at(unsigned int x, unsigned int y)
    11. {
    12. return (array_)[x][y];
    13. }
    14.  
    15. // fortran interface - transpose it
    16. T& operator()(unsigned int x, unsigned int y)
    17. {
    18. return (array_)[y][x];
    19. }
    20.  
    21. //pass the matrix to a fortran method accepting a T*
    22. operator T*()
    23. {
    24. return &(*array_)[0];
    25. }
    26.  
    27. private:
    28. T (*array_)[N];
    29. };
    To copy to clipboard, switch view to plain text mode 
    Quite clearly there IS a method 'at'. Funnily enough, I commented that this method to be used in cpp code, which is why I remarked that this method, and not operator()(...), should be used.

    Regards
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  4. The following user says thank you to amleto for this useful post:

    d_stranz (13th September 2012)

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,329
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to get a pointer for 2D array of QVector?

    Quite clearly there IS a method 'at'.
    And equally clear is the fact that I was the one up too early to be typing comments into the forum...

  6. #5
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    1

    Default Re: How to get a pointer for 2D array of QVector?

    Thank you for all replies and comments, given above. The issue regrading data-sharing between Fortran and C++ is resolved.

Similar Threads

  1. 2D array in Qt: QVector or QList
    By timmu in forum Newbie
    Replies: 8
    Last Post: 26th April 2017, 17:08
  2. QVector array declear
    By zhxys in forum Newbie
    Replies: 8
    Last Post: 2nd February 2011, 09:17
  3. Replies: 1
    Last Post: 4th August 2010, 17:13
  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

Tags for this Thread

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.