Weird syntax error with templates

Discussion in 'C++' started by James Aguilar, Dec 5, 2004.

  1. There is a syntax error in this code that I can't figure out.

    template <class T>
    std::eek:stream &operator <<(std::eek:stream &os, PriorityQueue<T> &q)
    {
    using namespace std;

    //g++ doesn't like the next line, says error before = operator.
    vector< HeapNode< T > >::iterator it = q.m_store.begin();

    while (it != q.m_store.end()) {
    os << *it++ << endl;
    }

    return os;
    }

    The actual error message is:

    "PriorityQueue.h: In function 'std::eek:stream& operator<<(std::eek:stream&,
    PriorityQueue<T>&)':
    PriorityQueue.h:228: error: parse error before '=' token"

    Any idea what could be causing it? "HeapNode< T >" is valid when used as a
    declaration, and if I change the vector declaration to "vector< int >" it
    also works (even though q.m_store is not actually a vector of ints but of
    Ts).

    Thanks,

    James
     
    James Aguilar, Dec 5, 2004
    #1
    1. Advertising

  2. "James Aguilar" <> wrote...
    > There is a syntax error in this code that I can't figure out.
    >
    > template <class T>
    > std::eek:stream &operator <<(std::eek:stream &os, PriorityQueue<T> &q)
    > {
    > using namespace std;
    >
    > //g++ doesn't like the next line, says error before = operator.
    > vector< HeapNode< T > >::iterator it = q.m_store.begin();


    'vector<HeapNode<T> >::iterator is not known to the compiler as a type.
    The reason is that it is dependent on 'T'. You need to help the compiler
    by suggesting that it's a type:

    typename vector<HeapNode<T> >::iterator it = q.m_store.begin();

    >
    > while (it != q.m_store.end()) {
    > os << *it++ << endl;
    > }
    >
    > return os;
    > }
    >
    > The actual error message is:
    >
    > "PriorityQueue.h: In function 'std::eek:stream& operator<<(std::eek:stream&,
    > PriorityQueue<T>&)':
    > PriorityQueue.h:228: error: parse error before '=' token"
    >
    > Any idea what could be causing it? "HeapNode< T >" is valid when used as
    > a declaration, and if I change the vector declaration to "vector< int >"
    > it also works (even though q.m_store is not actually a vector of ints but
    > of Ts).


    See inline with quoting.

    V
     
    Victor Bazarov, Dec 5, 2004
    #2
    1. Advertising

  3. James Aguilar wrote:

    > There is a syntax error in this code that I can't figure out.
    >
    > template <class T>
    > std::eek:stream &operator <<(std::eek:stream &os, PriorityQueue<T> &q)
    > {
    > using namespace std;
    >
    > //g++ doesn't like the next line, says error before = operator.
    > vector< HeapNode< T > >::iterator it = q.m_store.begin();
    >
    > while (it != q.m_store.end()) {
    > os << *it++ << endl;
    > }
    >
    > return os;
    > }
    >
    > The actual error message is:
    >
    > "PriorityQueue.h: In function 'std::eek:stream& operator<<(std::eek:stream&,
    > PriorityQueue<T>&)':
    > PriorityQueue.h:228: error: parse error before '=' token"
    >
    > Any idea what could be causing it? "HeapNode< T >" is valid when used as
    > a declaration, and if I change the vector declaration to "vector< int >"
    > it also works (even though q.m_store is not actually a vector of ints but
    > of Ts).


    I think you need to add the 'typename' keyword, like so:
    typename vector< HeapNode<T> >::iterator

    Daniel
     
    Daniel Mitchell, Dec 5, 2004
    #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. JKop
    Replies:
    3
    Views:
    483
  2. Generic Usenet Account
    Replies:
    3
    Views:
    831
    Generic Usenet Account
    Jul 14, 2005
  3. recover
    Replies:
    2
    Views:
    819
    recover
    Jul 25, 2006
  4. Student
    Replies:
    11
    Views:
    460
    James Kanze
    Dec 6, 2010
  5. Mark Richards
    Replies:
    3
    Views:
    319
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page