Vector of object

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
Object& myFunction(){
   vector<Object>myVect;
    .........
     .......
  return myVect;
}
int main(){
  vector<Object>v;
  
  v=myFunction();
  [?????]
  return 0;
}


I always get an error : no viable overloading '=' on line 10
this error is displayed only if the vector is not a vector of a primitive variable.
Last edited on
If you want the function to return a vector you should change the return type to vector<Object>.
Last edited on
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> "
I don't think that there is a base class called 'Object' in C++. Are you thinking of Java?
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>
using namespace std;

class Object{
};
vector<Object> myFunction(){
    vector<Object> myVect;
        return myVect;
}

int main(){
    vector<Object> v;
    v = myFunction();
}



Yes it makes sense !!! thank you.
I changed it to return a vector type instead of an object type
I wasn't thinking on java.
Topic archived. No new replies allowed.