Thread: template

  1. #1
    Unregistered
    Guest

    template

    I am trying to figure out what would be stored in "myList" at the end of this program? If anything since it looks like it removes everything from the front. Is that correct. If that is the outcome can I writea fuction to correct it?
    Katie


    template <class T>
    void printList(List<T> L);

    void main(){
    List<float> myList;

    myList.insertAtFront(3.4);
    myList.insertAtFront(16.7);
    printList(myList);
    myList.insertAtFront(100.1);
    }

    template <class T>
    void printList(List<T> L)
    {
    T answer;
    while(L.removeFromFront(answer)) {
    cout << answer << endl;
    }
    }

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Yes as your print function removes the nodes. You probably want it to print the answer without actually removing the node. In which case you'd take the head of the list walk through it, printing each value until you reached the end of the list. Something like -

    Node* iterator = head;

    while(iterator->next !=0)
    {
    //print answer
    iterator=iterator->next;
    }

    You'd have to convert the above to work with your code.
    zen

  3. #3
    Unregistered
    Guest

    outcome

    So will I have a 0 in mylist. Or will it not print it all if all the nodes are removed and there are no values?
    Thanks again
    Katie

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    A zero or null will mark the end of the list. I don't know if this is the case as you've only posted a bit of your code, but when you add a node to the end of a list you should set the pointer linking it to the next node to null.
    zen

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    Hi

    since you've not passed the list L as reference to your printList function
    Code:
    void printList(List<T> L) // here
    a copy of the list is created temporary and passes so all the elements from that copy are excluded from it and displayed.

    the original list is not affected and at the end of your function you should have all 3 elements in the list.

    damyan

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