output operator for vector

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rares Vernica

    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<<(ostr eam& 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
  • Ian Collins

    #2
    Re: output operator for vector

    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<<(ostr eam& out, const vector<Tv)
    {
    out << '[';
    for(vector<T>:: const_iterator it = v.begin(); it != v.end(); it++) {
    should be

    for(typename vector<T>::cons t_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.

    Comment

    • Victor Bazarov

      #3
      Re: output operator for vector

      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<<(ostr eam& out, const vector<Tv)
      It might also help to pass by a reference to const:

      ostream& operator<<(ostr eam& out, const vector<T& v)
      >{
      > out << '[';
      > for(vector<T>:: const_iterator it = v.begin(); it != v.end(); it++)
      >{
      >
      should be
      >
      for(typename vector<T>::cons t_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


      Comment

      • jhimcras@gmail.com

        #4
        Re: output operator for vector

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

        Comment

        • Rares Vernica

          #5
          Re: output operator for vector

          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<<(ostr eam& out, const vector<Tv)
          >
          It might also help to pass by a reference to const:
          >
          ostream& operator<<(ostr eam& out, const vector<T& v)
          >
          >>{
          >> out << '[';
          >> for(vector<T>:: const_iterator it = v.begin(); it != v.end(); it++)
          >>{
          >should be
          >>
          >for(typename vector<T>::cons t_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

          Comment

          • red floyd

            #6
            Re: output operator for vector

            jhimcras@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.

            Comment

            Working...