I was able to create a vector of object in a function, than return it.
but i couldn't display the content in the main function.
if there any way i could do it ?
ex:
I tried already to do that,
but it gave me an error saying :
"non-const lvalue reference to type 'Object' cannot bind to a value of unrelated type vector<Object> "
Because the instances of class Object returned by myFunction() are local variables that are destroyed when the function returns. So trying to assign references to these instances will not work unless the function returns a copy of the objects i.e by value, not reference:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include<iostream>
#include<vector>
usingnamespace std;
class Object{
};
vector<Object> myFunction(){
vector<Object> myVect;
return myVect;
}
int main(){
vector<Object> v;
v = myFunction();
}