0
return a vector
Hi, I wrote a function witch must return a vector. vector<int> myfunction() { vector<int> listPoint(5); for(int i=0; i<=5; ++i) { listPoint.push_back(i + 3); } return listPoint; } In the main i call the function like this: cout << myfunction(); in order to print the list of the points, but i have this error: main.cpp:20:24: error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
3 Answers
+ 1
The reason is because you cannot print the whole array with a cout. Your function is returning a vector listPoint and it must be saved in an int vector when you call it in main. Because you can only store returned values into their respective datatype variables. So doing this in main() would work:
vector<int> intVector = myfunction();
for(int i=0; i<intVector.size(); ++i)
cout << intVector.at(i); << endl;
+ 1
Okey good. Thank you so much.
I un derstand.
0
you are always welcome!