Solved

typedef within template class

Posted on 2013-12-27
5
533 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

636 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