| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"James Aguilar" |
| Date: |
05 Dec 2004 02:55:21 PM |
| Object: |
Weird syntax error with templates |
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
.
|
|
| User: "Victor Bazarov" |
|
| Title: Re: Weird syntax error with templates |
05 Dec 2004 02:59:56 PM |
|
|
"James Aguilar" <jfa1@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
.
|
|
|
|
| User: "Daniel Mitchell" |
|
| Title: Re: Weird syntax error with templates |
05 Dec 2004 03:04:12 PM |
|
|
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
.
|
|
|
|

|
Related Articles |
|
|