[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Generic iterator declaration

Hansel Stroem

9/26/2008 7:32:00 PM

Is this legitimate STL code ? Compiler seems not to like the iterator ...

template <typename Tn>
ostream& operator<<(ostream& out, vector<Tn> VV)
{
for ( vector<Tn>::iterator it = VV.begin(); it != VV.end(); ++it )
{
out << (*it);
}
return out << std::endl;
}


%icc BS.cc
BS.cc(151): error: expected a ";"
for ( vector<Tn>::iterator it = VV.begin(); it != VV.end(); ++it )
^

BS.cc(151): error: identifier "it" is undefined
for ( vector<Tn>::iterator it = VV.begin(); it != VV.end(); ++it )
^

compilation aborted for BS.cc (code 2)


2 Answers

Victor Bazarov

9/26/2008 7:37:00 PM

0

Hansel Stroem wrote:
> Is this legitimate STL code ? Compiler seems not to like the iterator ...

No, it's not. It's missing a keyword or two.

>
> template <typename Tn>
> ostream& operator<<(ostream& out, vector<Tn> VV)

You should look into passing the vector by a reference to const:

... (ostream& out, vector<Tn> const& VV)

> {
> for ( vector<Tn>::iterator it = VV.begin(); it != VV.end(); ++it )

Has to be

for ( typename vector<Tn>::iterator it = ...

(or, if you decide to pass the vector by a const ref,

for ( typename vector<Tn>::const_iterator it =

> {
> out << (*it);
> }
> return out << std::endl;
> }
>
>
> %icc BS.cc
> BS.cc(151): error: expected a ";"
> for ( vector<Tn>::iterator it = VV.begin(); it != VV.end(); ++it )
> ^
>
> BS.cc(151): error: identifier "it" is undefined
> for ( vector<Tn>::iterator it = VV.begin(); it != VV.end(); ++it )
> ^
>
> compilation aborted for BS.cc (code 2)
>
>

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

???

9/27/2008 2:06:00 AM

0

Victor Bazarov ??:

>> {
>> for ( vector<Tn>::iterator it = VV.begin(); it != VV.end(); ++it )
>
> Has to be
>
> for ( typename vector<Tn>::iterator it = ...
>
> (or, if you decide to pass the vector by a const ref,
>
> for ( typename vector<Tn>::const_iterator it =
>

right.
use typename here.
see the standard.