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

Parse Error in template class

P: n/a
This is a stripped down example that duplicates the problem I am
having. If I do it with concrete types, I can access the set's
iterator type correctly. If I make a template with generic types, I
can't get it right.

#include <set>

// concrete version. this compiles fine.
class it_works
{
private:
typedef std::pair<int, double> pair_t;
typedef std::set<pair_t> set_t;

set_t my_set;

public:
void func() {
set_t::iterator i;
std::set<pair_t>::iterator j;
std::set< std::pair<int, double> >::iterator k;
}
};

// generic version, templated on T1 and T2. it doesn't work.
template<typename T1, typename T2>
class doesnt_work
{
private:
typedef std::pair<T1, T2> pair_t;
typedef std::set<pair_t> set_t;

set_t my_set;

public:
void func() {
set_t::iterator i; // "parse error before `;' token"
std::set<pair_t>::iterator j; // same error
std::set< std::pair<T1, T2> >::iterator k; // same error
}
};

It seems to be just a stupid syntax error. If I replace one of the
lines with "BLAH something = _abcdefg_;", I get the same error. But I
can't figure out why I get the parsing errors on the generic version,
but not the concrete version. Testing a concrete version is usually
the process I go through to track down parse errors in a template, but
it didn't throw any light on things this time.

If it helps, I am using gcc-3.2.2, but I am fairly sure it is just a
syntax error.

--
Dave O'Hearn
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Gianni Mariani <gi*******@mariani.ws> wrote:
In a template it's sometimes impossible to tell if the token you are
about to parse is part of a type declaration or an identifier because it
is incomplete (by definition). To get around this, the keyword
"typename" is used to indicate to the compiler what it is.
Thanks. I had heard stories about the need to occasionally throw
"typename" around, but I figured it only happened in very weird
meta-template code and it would be years before I had to learn what it
meant. I guess not!
gcc 3.2.2 has some (many?) template parsing bugs. I used gcc 3.3 and
the messages were pretty good.


I am waiting for 3.4... just a few more months now!

--
Dave O'Hearn
Jul 19 '05 #2

P: n/a
Gianni Mariani <gi*******@mariani.ws> wrote:
In a template it's sometimes impossible to tell if the token you are
about to parse is part of a type declaration or an identifier because it
is incomplete (by definition). To get around this, the keyword
"typename" is used to indicate to the compiler what it is.
Thanks. I had heard stories about the need to occasionally throw
"typename" around, but I figured it only happened in very weird
meta-template code and it would be years before I had to learn what it
meant. I guess not!
gcc 3.2.2 has some (many?) template parsing bugs. I used gcc 3.3 and
the messages were pretty good.


I am waiting for 3.4... just a few more months now!

--
Dave O'Hearn
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.