How to print void

I rebuilt the vector class and I'm having problems with using cout to display a void vector.

1
2
3
while (!intVector.empty())
		cout << intVector.pop_back() << " ";
	cout << endl;


Where the underline is I am getting an error c:\testvector.cpp(17): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion). How can I fix this?
The 'pop_back' method merely removes the last element of the vector and reduces its size by one. It does not return anything (returning void means this). You are trying to print something which is 'NOTHING' i.e. 'void'.

If you are trying to print the array in reverse fashion, use the following code.

1
2
3
4
5
while (!intVector.empty ()) {
        cout << intVector.back () << " ";
        intVector.pop_back ();
}
cout << endl;


Not to say, this will remove all elements from the array. If you want to print it without removing its elements so that you can use them in future, you the following code.

1
2
3
4
vector<int>::reverse_iterator rit;
for (rit = intVector.rbegin (); rit != intVector.rend (); rit++)
        cout << *rit << " ";
cout << endl;


Hope this is enough. Enjoy coding :)
Topic archived. No new replies allowed.