trying to declare an iterator for a std::vector of template pointers

K

Ken Cecka

This is a contrived example to demonstrate a syntax problem I'm struggling
with:

#include <vector>

template <typename T>
class Class
{
};

template <typename T>
class Container
{
public:
typedef Class<T> * ClassPointer;
typedef std::vector<ClassPointer> ClassVector;
typedef ClassVector::iterator ClassVectorIterator;
};

int main(void)
{
return 0;
}

When I compile this code using g++ v3.4.1, I get the following errors:
/home/ceckak/test.cpp:16: error: type `std::vector<Class<T>*,
std::allocator<Class<T>*> >' is not derived from type `Container<T>'
/home/ceckak/test.cpp:16: error: ISO C++ forbids declaration of `iterator'
with no type
/home/ceckak/test.cpp:16: error: expected `;' before "ClassVectorIterator"

If I change the ClassPointer typedef to the following, the error goes away
typedef Class<int> * ClassPointer;

Or if I make Class a regular (non-template) class, the error goes away.

Or if I comment out the iterator typedef, the code compiles without errors.

Can anyone tell me if/how to declare an iterator to a vector of template
pointers?

Thanks in advance for any help!

Ken
 
M

Malte Starostik

Ken said:
This is a contrived example to demonstrate a syntax problem I'm struggling
with:

#include <vector>

template <typename T>
class Class
{
};

template <typename T>
class Container
{
public:
typedef Class<T> * ClassPointer;
typedef std::vector<ClassPointer> ClassVector;
typedef ClassVector::iterator ClassVectorIterator;
typedef typename ClassVector::iterator ClassVectorIterator;
};

int main(void)
int main()
// (void) to denote an empty argument list is C-ish
{
return 0;
}

When I compile this code using g++ v3.4.1, I get the following errors:
/home/ceckak/test.cpp:16: error: type `std::vector<Class<T>*,
std::allocator<Class<T>*> >' is not derived from type `Container<T>'
/home/ceckak/test.cpp:16: error: ISO C++ forbids declaration of `iterator'
with no type
/home/ceckak/test.cpp:16: error: expected `;' before "ClassVectorIterator"
Prior versions of gcc have accepted the code, but warned about implicit
typename being deprecated. Now it rejects the code completely, in full
accord with the standard.
If I change the ClassPointer typedef to the following, the error goes away
typedef Class<int> * ClassPointer;
Because then ClassVector is no longer dependent on the template parameter T.
Or if I make Class a regular (non-template) class, the error goes away. Dito.

Or if I comment out the iterator typedef, the code compiles without errors.
Obviously :)
Can anyone tell me if/how to declare an iterator to a vector of template
pointers?
Tell the compiler that ClassVector::iterator is a type name as I did above.

HTH,
Malte
 
S

Steffen Jakob

Am Sun, 27 Mar 2005 11:48:35 -0800 schrieb Ken Cecka:
#include <vector>

template <typename T>
class Class
{
};

template <typename T>
class Container
{
public:
typedef Class<T> * ClassPointer;
typedef std::vector<ClassPointer> ClassVector;
typedef ClassVector::iterator ClassVectorIterator;
};

int main(void)
{
return 0;
}
//
Can anyone tell me if/how to declare an iterator to a vector of template
pointers?

Yes:

typedef typename ClassVector::iterator ClassVectorIterator;
Thanks in advance for any help!

Greetings,
Steffen.
 
K

Ken Cecka

Malte said:
typedef typename ClassVector::iterator ClassVectorIterator;
int main()
// (void) to denote an empty argument list is C-ish

Prior versions of gcc have accepted the code, but warned about implicit
typename being deprecated. Now it rejects the code completely, in full
accord with the standard.

Because then ClassVector is no longer dependent on the template parameter
T.

Obviously :)

Tell the compiler that ClassVector::iterator is a type name as I did
above.

HTH,
Malte

Thanks both of you for the quick response. Adding typename solved the
problem. I'll have to do a little more reading up on when typename is
required, but I think I get the gist of it.

Ken
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top