Avatar of Rothbard
Rothbard
Flag for United Kingdom of Great Britain and Northern Ireland asked on

typedef within template class

I have a vector class with the following typedefs

template <class T> class Vec
{
public:
	// definitions
	typedef T* iterator;
	typedef const T* const_iterator;
	typedef T value_type;
	typedef size_t size_type;
	typedef std::ptrdiff_t difference_type;
	typedef T& reference;
	typedef const T& const_reference;
[...]}

Open in new window

Everything works fine, but when I want to implement an erase method to mimic the STL one, I find that I can only use iterator instead of T* (as per the typedef above) if I include the function definition within the Vec class definition. If on the other hand I only declare erase within the class definition as follows:

iterator erase(iterator it);

Open in new window


Then I can't define it externally in Vec.cpp as

template <class T> iterator Vec<T>::erase(iterator it) {[...]}

Open in new window

but I need to use T* instead of iterator:

template <class T> T* Vec<T>::erase(T* it) {[...]}

Open in new window

which sort of defeats the purpose of using the typedef.

Why does this happen? Is there any way around it?
C++

Avatar of undefined
Last Comment
Rothbard

8/22/2022 - Mon
jkr

In your case, 'iterator' has global scope, not the scope of 'Vec<T>' - did you mean

template<class T> Vec<T>::iterator Vec<T>::erase(Vec<T>::iterator it) { /* ... */ }

Open in new window


?
Rothbard

ASKER
Sorry, that's what I meant. However, if I do that I get the following warning message:

Warning      1      warning C4346: 'Vec<T>::iterator' : dependent name is not a type      

which is followed by several errors, some of which I have copied below.

Error      2      error C2143: syntax error : missing ';' before 'Vec<T>::erase'
Error      3      error C2065: 'T' : undeclared identifier
Error      4      error C2923: 'Vec' : 'T' is not a valid template type argument for parameter 'T'
Error      5      error C2146: syntax error : missing ')' before identifier 'it'
Error      6      error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error      7      error C2350: 'Vec<T>::erase' is not a static member      
Error      8      error C2059: syntax error : ')'
ASKER CERTIFIED SOLUTION
evilrix

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
evilrix

Oh, and you will probably find that implementing iterators is a lot simpler if you use the standard iterator and iterator traits framework.

http://www.cplusplus.com/reference/iterator/iterator_traits/
http://www.cplusplus.com/reference/iterator/iterator/
Your help has saved me hundreds of hours of internet surfing.
fblack61
Rothbard

ASKER
Thanks for the replies.