By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,914 Members | 1,706 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,914 IT Pros & Developers. It's quick & easy.

Weird syntax error with templates

P: n/a
There is a syntax error in this code that I can't figure out.

template <class T>
std::ostream &operator <<(std::ostream &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::ostream& operator<<(std::ostream&,
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
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"James Aguilar" <jf**@cec.wustl.edu> wrote...
There is a syntax error in this code that I can't figure out.

template <class T>
std::ostream &operator <<(std::ostream &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::ostream& operator<<(std::ostream&,
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
Jul 22 '05 #2

P: n/a
James Aguilar wrote:
There is a syntax error in this code that I can't figure out.

template <class T>
std::ostream &operator <<(std::ostream &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::ostream& operator<<(std::ostream&,
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

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.