Results 1 to 2 of 2

Thread: Problem converting QVector of user defined object to a std::vector of the same object

  1. #1
    Join Date
    Jun 2011
    Posts
    17
    Qt products
    Qt3
    Platforms
    MacOS X

    Default Problem converting QVector of user defined object to a std::vector of the same object

    I have a QVector of user defined object and the function I am passing the vector to requires vector<Object> &ObjectVector as the only parameter. Currently what I am doing is calling QVector.toStdVector() as I pass it as a reference to the function. I believe I am doing something fundamentaly wrong and it is fairly simple to fix but I just can't get it.

    This is the error message: error: no matching function for call to 'SetVector(std::vector<Object, std::allocator<Object> >)'

  2. #2
    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: Problem converting QVector of user defined object to a std::vector of the same ob

    The function accepts a vector by non-const reference implying that it wants to modify it. You are passing a temporary object returned from toStdVector() and this is likely the problem. Create an explicit vector to pass in. With my compiler:
    Qt Code:
    1. struct A {};
    2.  
    3. void SetVector(std::vector<A> &v) {
    4. }
    5.  
    6. QVector<A> a;
    7. SetVector(a.toStdVector());
    8. // generates
    9. // main.cpp:14:30: error: invalid initialization of non-const reference of type ‘std::vector<A>&’ from an rvalue of type ‘std::vector<A>’
    10. // main.cpp:6:6: error: in passing argument 1 of ‘void SetVector(std::vector<A>&)’
    11.  
    12. std::vector<A> b = a.toStdVector();
    13. SetVector(b);
    14. // compiles fine.
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 4
    Last Post: 15th July 2011, 18:31
  2. Replies: 2
    Last Post: 6th May 2011, 06:56
  3. Replies: 4
    Last Post: 20th August 2010, 13:07
  4. Switch case in Qvector object
    By rk0747 in forum Qt Programming
    Replies: 1
    Last Post: 3rd June 2010, 07:35
  5. Replies: 4
    Last Post: 26th June 2007, 19:19

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.