Solved

typedef within template class

Posted on 2013-12-27
5
522 Views
Last Modified: 2013-12-31
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?
0
Comment
Question by:Rothbard
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39742239
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


?
0
 

Author Comment

by:Rothbard
ID: 39742267
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 : ')'
0
 
LVL 40

Accepted Solution

by:
evilrix earned 350 total points
ID: 39742352
>> Then I can't define it externally in Vec.cpp as
Well, you can't do that anyway since the definition of the function needs to be seen at the point of instantiation. You need to define it in the header. You can define it in the header after the main class if you wish to separate the declaration and implementation but you do need to put it in the header.

See my following article for more details.

http://evilrix.com/2012/12/02/separating-c-template-declaration-and-implementation/

>> which is followed by several errors, some of which I have copied below.
Did you still leave the function declaration in the body of the class?

I am, also, wondering why you are implementing a vector when the STL has one already? Is this an academic exercise?
0
 
LVL 40

Expert Comment

by:evilrix
ID: 39742356
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/
0
 

Author Comment

by:Rothbard
ID: 39748332
Thanks for the replies.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual c++ and text files 7 69
C Language combined operators 28 109
Problem with SqlConnection 4 171
Safe conversion? 4 69
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question