return a vector | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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>&&'

29th Nov 2016, 1:41 AM
Attitude
Attitude - avatar
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;
29th Nov 2016, 3:36 AM
Mohammed Maaz
Mohammed Maaz - avatar
+ 1
Okey good. Thank you so much. I un derstand.
29th Nov 2016, 4:37 AM
Attitude
Attitude - avatar
0
you are always welcome!
29th Nov 2016, 4:38 AM
Mohammed Maaz
Mohammed Maaz - avatar