473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

output operator for vector

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<Tv)
{
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::ostream& operator<<(std::ostream&,
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
Jan 31 '07 #1
5 2488
Rares Vernica wrote:
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<Tv)
{
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.
--
Ian Collins.
Jan 31 '07 #2
Ian Collins wrote:
Rares Vernica wrote:
>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<Tv)
It might also help to pass by a reference to const:

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.
The explanation for 'typename' is that 'const_iterator' is a dependent
name. See FAQ.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 31 '07 #3
When I compile that code in Visual C++ 6.0
There are no error.

Jan 31 '07 #4
Thank you very much,
Ray

Victor Bazarov wrote:
Ian Collins wrote:
>Rares Vernica wrote:
>>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<Tv)

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

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.

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

V
Jan 31 '07 #5
jh******@gmail.com wrote:
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.
Jan 31 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Makhno | last post by:
Hello, Why does my cast from Vector<class Float> to Vector<float> not work? It won't compile, template<class Float> class Vector { public: Vector(Float x1,Float y1,Float...
4
by: Master of C++ | last post by:
Hi, This is a simple question. In the following example, .. class Vector .. { .. private: .. int *Numbers; .. int vLength; ..
4
by: Mark Stijnman | last post by:
A while ago I posted a question about how to get operator behave differently for reading and writing. I basically wanted to make a vector that can be queried about whether it is modified recently...
2
by: George | last post by:
My program is supposed to draw lines given from certain vertices that are given to me. the colors from the lines are also defined. It should display on a black screen. I am not sure how to do this...
2
by: Mr Dyl | last post by:
I'm having a few issues porting Windows code to Linux (VS.Net 2003 to GCC 4.0.1). So this probably has something to do with VS not catching non-compliant code. I'd like to simplify this problem,...
17
by: benben | last post by:
Given a class template Vector<>, I would like to overload operator +. But I have a hard time deciding whether the return type should be Vector<U> or Vector<V>, as in: template <typename U,...
0
by: santana | last post by:
Hi I've created a class to be used with stl vector. I'm having a hard time decipher the error message outpout by g++... burlen@quaoar:~/hdf52vtk_dev/src$ g++ junk.cpp GridPoint.o...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
1
by: fabian.lim | last post by:
Hi all, Im having a problem with my code. Im programming a vector class, and am trying to overload the () operator in 2 different situations. The first situation is to assign values, e.g. Y =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.