Generic iterator declaration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hansel Stroem

    Generic iterator declaration

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

    template <typename Tn>
    ostream& operator<<(ostr eam& out, vector<TnVV)
    {
    for ( vector<Tn>::ite rator 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>::ite rator it = VV.begin(); it != VV.end(); ++it )
    ^

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

    compilation aborted for BS.cc (code 2)


  • Victor Bazarov

    #2
    Re: Generic iterator declaration

    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<<(ostr eam& out, vector<TnVV)
    You should look into passing the vector by a reference to const:

    ... (ostream& out, vector<Tnconst& VV)
    {
    for ( vector<Tn>::ite rator it = VV.begin(); it != VV.end(); ++it )
    Has to be

    for ( typename vector<Tn>::ite rator it = ...

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

    for ( typename vector<Tn>::con st_iterator it =
    {
    out << (*it);
    }
    return out << std::endl;
    }
    >
    >
    %icc BS.cc
    BS.cc(151): error: expected a ";"
    for ( vector<Tn>::ite rator it = VV.begin(); it != VV.end(); ++it )
    ^
    >
    BS.cc(151): error: identifier "it" is undefined
    for ( vector<Tn>::ite rator 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

    Comment

    • =?UTF-8?B?5Lmm5ZGG5b2t?=

      #3
      Re: Generic iterator declaration

      Victor Bazarov 写道:
      >{
      > for ( vector<Tn>::ite rator it = VV.begin(); it != VV.end(); ++it )
      Has to be

      for ( typename vector<Tn>::ite rator it = ...

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

      for ( typename vector<Tn>::con st_iterator it =
      right.
      use typename here.
      see the standard.

      Comment

      Working...