Weird syntax error with templates



 DEVELOP > c-Plus-Plus > Weird syntax error with templates

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER