Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

typedef within template class

Posted on 2013-12-27
5
Medium Priority
?
543 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 1400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

877 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