compiler errors on iterator

Discussion in 'C++' started by frankz, Aug 16, 2010.

  1. frankz

    frankz Guest

    Below is a piece of code that I abstracted from a C++ book but doesn't
    compile. If I substitute template class T with int type, it compiles.
    Don't know why?

    #include <iostream>

    #include <iterator> // for iterator

    using std::cout;

    using std::cin;

    using std::endl;





    #include <vector>



    template <class T>

    void printVector( const std::vector<T> &vec)

    {

    std::vector<T>::const_iterator i;

    for (i=vec.begin(); i!= vec.end();i++)

    cout << *i << " ";

    }

    g++ -Wall -o "stl" "stl.cpp"
    stl.cpp: In function ‘void printVector(const std::vector<T,
    std::allocator<_CharT> >&)’:
    stl.cpp:13: error: expected ‘;’ before ‘i’
    stl.cpp:14: error: ‘i’ was not declared in this scope
    Compilation failed.
     
    frankz, Aug 16, 2010
    #1
    1. Advertising

  2. frankz

    frankz Guest

    On Aug 16, 11:25 am, "Leigh Johnston" <> wrote:
    > "frankz" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > Below is a piece of code that I abstracted from a C++ book but doesn't
    > > compile. If I substitute template class T with int type, it compiles.
    > > Don't know why?

    >
    > > #include <iostream>

    >
    > > #include <iterator> // for iterator

    >
    > > using std::cout;

    >
    > > using std::cin;

    >
    > > using std::endl;

    >
    > > #include <vector>

    >
    > > template <class T>

    >
    > > void printVector( const std::vector<T> &vec)

    >
    > > {

    >
    > >    std::vector<T>::const_iterator i;

    >
    > >    for (i=vec.begin(); i!= vec.end();i++)

    >
    > >        cout << *i << " ";

    >
    > > }

    >
    > > g++ -Wall -o "stl" "stl.cpp"
    > > stl.cpp: In function ‘void printVector(const std::vector<T,
    > > std::allocator<_CharT> >&)’:
    > > stl.cpp:13: error: expected ‘;’ before ‘i’
    > > stl.cpp:14: error: ‘i’ was not declared in this scope
    > > Compilation failed.

    >
    > Try
    >
    > typename std::vector<T>::const_iterator i;
    >
    > /Leigh


    Thanks a lot! Can you explain why? btw The original code is from "C++
    How to program"
     
    frankz, Aug 16, 2010
    #2
    1. Advertising

  3. frankz

    red floyd Guest

    On 8/16/2010 10:52 AM, frankz wrote:
    > On Aug 16, 11:25 am, "Leigh Johnston"<> wrote:
    >> "frankz"<> wrote in message
    >>
    >> news:...
    >>
    >>
    >>
    >>> Below is a piece of code that I abstracted from a C++ book but doesn't
    >>> compile. If I substitute template class T with int type, it compiles.
    >>> Don't know why?

    >>
    >>> #include<iostream>

    >>
    >>> #include<iterator> // for iterator

    >>
    >>> using std::cout;

    >>
    >>> using std::cin;

    >>
    >>> using std::endl;

    >>
    >>> #include<vector>

    >>
    >>> template<class T>

    >>
    >>> void printVector( const std::vector<T> &vec)

    >>
    >>> {

    >>
    >>> std::vector<T>::const_iterator i;

    >>
    >>> for (i=vec.begin(); i!= vec.end();i++)

    >>
    >>> cout<< *i<< " ";

    >>
    >>> }

    >>
    >>> g++ -Wall -o "stl" "stl.cpp"
    >>> stl.cpp: In function ‘void printVector(const std::vector<T,
    >>> std::allocator<_CharT> >&)’:
    >>> stl.cpp:13: error: expected ‘;’ before ‘i’
    >>> stl.cpp:14: error: ‘i’ was not declared in this scope
    >>> Compilation failed.

    >>
    >> Try
    >>
    >> typename std::vector<T>::const_iterator i;
    >>
    >> /Leigh

    >
    > Thanks a lot! Can you explain why? btw The original code is from "C++
    > How to program"


    Yes, It's a FAQ. Please see the FAQ, in particular FAQ 35.18

    http://www.parashift.com/c -faq-lite
     
    red floyd, Aug 16, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,028
    Mark Goldin
    Jan 17, 2004
  2. Hendrik Maryns
    Replies:
    18
    Views:
    1,467
  3. greg
    Replies:
    6
    Views:
    481
    Dietmar Kuehl
    Jul 17, 2003
  4. Replies:
    6
    Views:
    694
    Jim Langston
    Oct 30, 2005
  5. Steven D'Aprano

    What makes an iterator an iterator?

    Steven D'Aprano, Apr 18, 2007, in forum: Python
    Replies:
    28
    Views:
    1,267
    Steven D'Aprano
    Apr 20, 2007
Loading...

Share This Page