View Full Version : Problem converting QVector of user defined object to a std::vector of the same object

8th November 2011, 21:30
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> >)'

8th November 2011, 23:27
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:

struct A {};

void SetVector(std::vector<A> &v) {

QVector<A> a;
// generates
// main.cpp:14:30: error: invalid initialization of non-const reference of type ‘std::vector<A>&’ from an rvalue of type ‘std::vector<A>’
// main.cpp:6:6: error: in passing argument 1 of ‘void SetVector(std::vector<A>&)’

std::vector<A> b = a.toStdVector();
// compiles fine.