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

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

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Ken Cecka schrieb:
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
Jul 23 '05 #2

P: n/a
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.
Jul 23 '05 #3

P: n/a
Malte Starostik wrote:
Ken Cecka schrieb:
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


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
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.