Weird syntax error with templates

J

James Aguilar

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
 
V

Victor Bazarov

James Aguilar said:
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 said:
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
 
D

Daniel Mitchell

James said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top