stl list

This is a discussion on stl list within the C++ Programming forums, part of the General Programming Boards category; What is wrong with this: Code: template<class T> void printList(const List<T> &list) { ... } I want to be able ...

  1. #1
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195

    stl list

    What is wrong with this:

    Code:
    template<class T>
    void printList(const List<T> &list)
    {
    ...
    }
    I want to be able to create a template function using the list container, so i can print lists of ints, doubles, strings, etc....

    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

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    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);
    }

  3. #3
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    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

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    The reason your original one didn't work is this.

    You had this declaration:

    Code:
    void printList(const List<T> &list)
    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> &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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Simple list code
    By JimpsEd in forum C Programming
    Replies: 1
    Last Post: 05-24-2006, 02:01 PM
  4. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08:01 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21