Thread: Using typedefs within classes.

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Using typedefs within classes.

    I'm adding a function to a template class which returns a type that is defined by the class. Now the function is getting a little long so I want to declare it in the class declaration but define it outside. Here is what I'm talking about


    Code:
     //here is where the type is defined
    template <class T> class Vec {
    public:
    	typedef T* iterator;
    	typedef const T* const_iterator;
    ...

    other member functions that return iterators are defined and declared at the same time like this...
    Code:
     
    iterator begin() { return data; }
    ...
    const_iterator end() const { return avail; }

    and here is the function declaration and definition I created for this class
    Code:
    //declared within the class scope
    const_iterator end() const { return avail; }
    ...
    
    
    //defined outside the scope, but in same header file of course
    template <class T>
    iterator Vec<T>::erase(const T que)
    {
    	iterator result = std::find(data, avail, que);
    	alloc.destroy(result);
    	if (result == avail && avail == limit)
    	{
    		avail = limit = avail - 1;
    	}
    	if (result == avail && avail != limit)
    		avail --;
    	else
    	{
    		iterator before = result;
    		result++;
    		std::copy(result, avail, before);
    		avail--;
    	}
    	return result;
    }
    But I'm getting the error
    Code:
    Vec.h:142: error: expected constructor, destructor, or type conversion before "Vec"
    Vec.h:142: error: expected `;' before "Vec"
    both errors highlight "iterator Vec<T>::erase(const T que)"

    now of course when I change iterator to T* it eliminates errors. Is there a workaround so that I can put user defined types in the header outside of the class scope, or is that just hte way it is?

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    template <class T>
    Vec<T>::iterator Vec<T>::erase(const T que)
    signature under construction

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    yeah I tried that before and it doesn't work.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    then perhaps:

    template <class T>
    typename Vec<T>::iterator Vec<T>::erase(const T que)
    signature under construction

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I forgot all about that keyword. but I never understood it all too much when I first read about it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Using typedefs from other classes
    By renanmzmendes in forum C++ Programming
    Replies: 5
    Last Post: 03-09-2008, 08:12 AM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  4. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  5. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM