Thread: Template troubles II

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    25

    Template troubles II

    Greetings, everyone, and don't laugh at me for being a poor novice programmer
    I'm trying to implement a (very specific) Database class, and I think it'd be nice to be able use some of the generic algorithms on it, like find. So here's my code (stripped to the barest minimum which won't compile):

    Code:
    #include <vector>
    
    using namespace std;
    
    template<class Type>
    class Database {
    private:
    	vector<Type> _data;
    
    public:
    	vector<Type>::iterator begin()  {
    		return _data.begin(); 
    	}
    
    	vector<Type>::iterator end() {
    		return _data.end(); 
    	}
    };
    The compiler says things like 'std::vector<_Ty>::iterator' : dependent name is not a type prefix with 'typename' to indicate a type. Obviously, it has a grudge against me for something.
    Or does anyone think otherwise?

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    listen to your compiler
    Code:
    template<class Type>
    class Database {
    private:
    	vector<Type> _data;
    
    public:
    	typename vector<Type>::iterator begin()  {
    		return _data.begin(); 
    	}
    
    	typename vector<Type>::iterator end() {
    		return _data.end(); 
    	}
    };
    Why you need typename?
    Because without it it thinks iterator is a static member of class vector<Type> and not a type in its own right.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    24
    you need to add the 'typename' qualifier when referencing a typedef external to a template.

    Code:
    	typename vector<Type>::iterator begin()  {
    		return _data.begin(); 
    	}
    an easier way is to define the typedef for your template:

    Code:
    template<class Type>
    class Database {
    private:
    	vector<Type> _data;
       typedef typename vector<Type>::iterator iterator;
    
    public:
    	iterator begin()  {
    		return _data.begin(); 
    	}
    
    	iterator end() {
    		return _data.end(); 
    	}
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM