What is wrong with this:
I want to be able to create a template function using the list container, so i can print lists of ints, doubles, strings, etc....Code:template<class T> void printList(const List<T> &list) { ... }
thanks
What is wrong with this:
I want to be able to create a template function using the list container, so i can print lists of ints, doubles, strings, etc....Code:template<class T> void printList(const List<T> &list) { ... }
thanks
'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition
Works good for me
Code:#include <iostream> #include <list> using namespace std; template<class T> void PrintList( const list<T>& l ) { list<T>::const_iterator walker = l.begin(); while( walker != l.end() ) { cout << *walker++ << endl; } } int main() { list<int> l1; l1.push_front(1); l1.push_front(2); l1.push_front(3); l1.push_front(4); PrintList(l1); list<char *> l2; l2.push_front("one"); l2.push_front("two"); l2.push_front("three"); l2.push_front("four"); PrintList(l2); list<char> l3; l3.push_front('a'); l3.push_front('b'); l3.push_front('c'); l3.push_front('d'); PrintList(l3); }
thanks
'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition
The reason your original one didn't work is this.
You had this declaration:
What is List? This should be changed to list, then your actual variable should be something other than the type name.Code:void printList(const List<T> &list)
Code:void printList(const list<T> &myList)
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers