Thanks for your reply as well. The program I sent was just to describe
my problem.
compiler behavior).
I can't say that today I didn't learn anything...
Fab wrote:
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;
}