Thread: Templated function and stl

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    2

    Templated function and stl

    I defined a templated function as below:

    Code:
    template<class T>
    list<T *>::iterator FindInternalByID(list<T *> *_List, int _ID){
    	list<T *>::iterator i; 
    	for (i = _List->begin(); i != _List->end(); i++){
    		if ((*i)->GetID() == _ID){
    			return i;
    		}
    	}
    
    	return _List->end();
    };
    Compiler gives an error:
    error C2146: syntax error : missing ';' before identifier 'FindInternalByID'
    I tried some other definitions and found that if i dont return an iterator like the above, no error occurs.
    How can I correct this?
    Last edited by cemuzunlar; 12-24-2006 at 12:16 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You should prefix typenames based on templated types with the "typename" keyword.

    Also, have you included <list> and added an using directive or declaration for the std namespace?

    Finally, names with underscores followed by an uppercase letter (like _List) are reserved for the implementation. Don't mimic the naming style of your library implementation, stick to legal names.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    2
    Thanks. "typename" fixed the error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with stl vectors function push_back
    By _lazycat_ in forum C++ Programming
    Replies: 10
    Last Post: 06-05-2009, 02:53 PM
  2. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 04:30 AM
  3. swap function of STL container
    By George2 in forum C++ Programming
    Replies: 11
    Last Post: 03-29-2008, 02:53 AM
  4. Not sure what STL function I am looking for
    By BobMcGee123 in forum C++ Programming
    Replies: 3
    Last Post: 07-04-2006, 04:46 PM
  5. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM