Template problem with g++

F

Fab

All,
I need your help understanding why the following code does *NOT*
compile with G++ (tested with gcc 3.x and 4.1.x):
---------------------------------------------------------------------
template<class T>
class myvector { // Just a simplification of std::vector
class iterator {
T a;
iterator &operator++();
};

iterator &begin();
iterator &end();
};

template<class T>
struct mytype {
T a;
T b;
};

template<class T>
class Foo {

int abc() {
myvector<mytype<T> > foo;
// DOES work:
//for (myvector<mytype<int> >::iterator iter = foo.begin();
iter != foo.end(); ++iter) {

// DOES NOT work:
for (myvector<mytype<T> >::iterator iter = foo.begin(); iter !
= foo.end(); ++iter) {
// ...
}
return 0;
}
};
---------------------------------------------------------------------

In method Foo::abc if I specify int as template argument for mytype,
everything works, but if I want to use T (the template argument of Foo
class), the compiler reports this error:

foo.cpp: In member function 'int Foo<T>::abc()':
foo.cpp:28: error: expected `;' before 'iter'
foo.cpp:28: error: 'iter' was not declared in this scope

I haven't personally testes, but apparently the Microsoft compiler
(Visual Studio 2005) accept that code.

Any help is appreciated!
Thanks,
Fabrizio
 
M

Mark P

Fab said:
All,
I need your help understanding why the following code does *NOT*
compile with G++ (tested with gcc 3.x and 4.1.x):
---------------------------------------------------------------------
template<class T>
class myvector { // Just a simplification of std::vector
class iterator {
T a;
iterator &operator++();
};

iterator &begin();
iterator &end();
};

template<class T>
struct mytype {
T a;
T b;
};

template<class T>
class Foo {

int abc() {
myvector<mytype<T> > foo;
// DOES work:
//for (myvector<mytype<int> >::iterator iter = foo.begin();
iter != foo.end(); ++iter) {

// DOES NOT work:
for (myvector<mytype<T> >::iterator iter = foo.begin(); iter !
= foo.end(); ++iter) {
 
P

Piyo

Fab said:
All,
I need your help understanding why the following code does *NOT*
compile with G++ (tested with gcc 3.x and 4.1.x):
---------------------------------------------------------------------
template<class T>
class myvector { // Just a simplification of std::vector
class iterator {
T a;
iterator &operator++();
};

iterator &begin();
iterator &end();
};

template<class T>
struct mytype {
T a;
T b;
};

template<class T>
class Foo {

int abc() {
myvector<mytype<T> > foo;
// DOES work:
//for (myvector<mytype<int> >::iterator iter = foo.begin();
iter != foo.end(); ++iter) {

// DOES NOT work:
for (myvector<mytype<T> >::iterator iter = foo.begin(); iter !
= foo.end(); ++iter) {
// ...
}
return 0;
}
};
---------------------------------------------------------------------

In method Foo::abc if I specify int as template argument for mytype,
everything works, but if I want to use T (the template argument of Foo
class), the compiler reports this error:

foo.cpp: In member function 'int Foo<T>::abc()':
foo.cpp:28: error: expected `;' before 'iter'
foo.cpp:28: error: 'iter' was not declared in this scope

I haven't personally testes, but apparently the Microsoft compiler
(Visual Studio 2005) accept that code.

Any help is appreciated!
Thanks,
Fabrizio

Whew! Now this will work. The main problem was the typename
keyword was required. To get the program to be errorless was
a another story :)

HTH

-----------------------------------------------------------
template<class T>
class myvector
{ // Just a simplification of std::vector
public:
class iterator
{
public:
T a;
bool operator!=( const iterator &b ) { return true; }
iterator & operator++() { return *this; }
iterator operator++( int ) { return *this; }
};

iterator begin() { return iterator(); }
iterator end() { return iterator(); }
};

template<class T>
struct mytype {
T a;
T b;
};

template<class T>
class Foo
{
public:
void abc()
{
myvector<mytype<T> > foo;
// DOES work:
//for (myvector<mytype<int> >::iterator iter = foo.begin();
//iter != foo.end(); ++iter) {

// DOES NOT work:
for (typename myvector<mytype<T> >::iterator iter = foo.begin();
iter != foo.end(); ++iter)
{

}

}
};

int
main()
{
Foo<int> test;
test.abc();
return 0;
}
 
F

Fab

Piyo,
Thanks for your reply as well. The program I sent was just to describe
my problem.
In my application at the end I use std::vector, but in my test I
wanted to rule out any possible problem related to how STL are
implemented in my distribution (in particular, I wanted to focus on
compiler behavior).

I can't say that today I didn't learn anything...

Thanks again!
Fab
 
M

Michael

for( typename myvector<mytype<T> >::iterator...

This occurs so frequently that I propose the 80/20 rule:
80% of all template problems posted to comp.lang.c++ are due to a
missing 'typename' keyword. 20% are other problems.

Michael
 
A

Alan Johnson

Michael said:
This occurs so frequently that I propose the 80/20 rule:
80% of all template problems posted to comp.lang.c++ are due to a
missing 'typename' keyword. 20% are other problems.

Michael

Somewhat surprisingly, we don't have anything about this in the FAQ
(that I can find). 35.18 sort of covers it, but not in a way that
someone looking for the answer to this variation of the problem would be
likely to understand.
 
M

Mark P

Alan said:
Somewhat surprisingly, we don't have anything about this in the FAQ
(that I can find). 35.18 sort of covers it, but not in a way that
someone looking for the answer to this variation of the problem would be
likely to understand.

Indeed, when I wrote my reply, I began, "This is an FAQ," and went off
to search for the relevant link item. Then I discovered the same thing
you did. (Now I didn't try very hard with the search feature so maybe
it's categorized under one of the miscellaneous chapters.)
 
M

Marcus Kwok

Mark P said:
Indeed, when I wrote my reply, I began, "This is an FAQ," and went off
to search for the relevant link item. Then I discovered the same thing
you did. (Now I didn't try very hard with the search feature so maybe
it's categorized under one of the miscellaneous chapters.)

I agree. I just sent an email to Marshall Cline suggesting that it be
added, and pointed him to this thread.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top