output operator for vector

R

Rares Vernica

Hi,

I am trying to write a template that will take any type of vector and
output it in a certain way. Here is my code:

#include <ostream>
#include <vector>
#include <iterator>

using namespace std;

template <class T>
ostream& operator<<(ostream& out, const vector<T> v)
{
out << '[';
for(vector<T>::const_iterator it = v.begin(); it != v.end(); it++) {
if (it != v.begin()) {
out << ", ";
}
out << *it;
}
out << ']';

return out;
}

I get the following error:

output.h: In function `std::eek:stream& operator<<(std::eek:stream&,
std::vector<T, std::allocator<_CharT> >)':
output.h:22: error: expected `;' before "it"
output.h:22: error: `it' undeclared (first use this function)
output.h:22: error: (Each undeclared identifier is reported only once
for each function it appears in.)
make: *** [output.o]

Do you have any idea what the problem might me?

Thank you very much,
Ray
 
I

Ian Collins

Rares said:
Hi,

I am trying to write a template that will take any type of vector and
output it in a certain way. Here is my code:

#include <ostream>
#include <vector>
#include <iterator>

using namespace std;

template <class T>
ostream& operator<<(ostream& out, const vector<T> v)
{
out << '[';
for(vector<T>::const_iterator it = v.begin(); it != v.end(); it++) {

should be

for(typename vector<T>::const_iterator it = v.begin(); it != v.end();
++it) {

Note I changed it++ to ++it, if an iterator isn't a simple pointer, ++it
is more efficient.
 
V

Victor Bazarov

It might also help to pass by a reference to const:

ostream& operator said:
{
out << '[';
for(vector<T>::const_iterator it = v.begin(); it != v.end(); it++)
{

should be

for(typename vector<T>::const_iterator it = v.begin(); it != v.end();
++it) {

Note I changed it++ to ++it, if an iterator isn't a simple pointer,
++it is more efficient.

The explanation for 'typename' is that 'const_iterator' is a dependent
name. See FAQ.

V
 
R

Rares Vernica

Thank you very much,
Ray

Victor said:
It might also help to pass by a reference to const:

ostream& operator said:
{
out << '[';
for(vector<T>::const_iterator it = v.begin(); it != v.end(); it++)
{
should be

for(typename vector<T>::const_iterator it = v.begin(); it != v.end();
++it) {

Note I changed it++ to ++it, if an iterator isn't a simple pointer,
++it is more efficient.

The explanation for 'typename' is that 'const_iterator' is a dependent
name. See FAQ.

V
 
R

red floyd

When I compile that code in Visual C++ 6.0
There are no error.

VC 6 is incorrect. As others have posted, a compliant compiler should
reject it without the typename keyword.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,206
Latest member
SybilSchil

Latest Threads

Top