Solved

typedef within template class

Posted on 2013-12-27
5
515 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

  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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now