Please help with template node class

This is a discussion on Please help with template node class within the C++ Programming forums, part of the General Programming Boards category; Okay for my project I had to create a template class for nodes in a linked list. The code: Code: ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    29

    Please help with template node class

    Okay for my project I had to create a template class for nodes in a linked list. The code:
    Code:
    template <class T>
    class node{
    
      public:
             node(){linkfield = NULL;}
             node(T d, node *lf){datafield = d; linkfield = lf;}
      
      T data(){return datafield;}
      node * link(){return linkfield;}
    
      void setData(T d){datafield = d;}
      void setLink(node *lf){linkfield = lf;}
       
     private: 
               T datafield;
                node *linkfield;
    };
    now I am using this class to make 2 lists, one of a resident object and one of a clothing object (this project is for doing laundry for a household). The problem I am having is in this function which removes an item of clothing from the linked list:

    Code:
    void Laundry::wearItem(Clothing c){
       node <Clothing> * tmp;
       if(c_head == NULL) return;
       if(c_head->data() == c)
       {
          tmp = c_head;
          c_head = c_head->link();
          delete tmp;
       }
       else
       {
          tmp = c_head->link();
          node <Clothing> * previous = c_head;
          while(tmp != NULL && tmp->data() != c){
             tmp = tmp->link();
             previous = previous->link();
          }
          if(tmp == NULL)
             cout << "That item was not found in the list.\n";
          else if(tmp->data() == c)
          {
             previous->setLink(tmp->link());
             delete tmp;
          }
       }
    }
    any time i try to compare the data in a node to an object...

    Code:
    if(c_head->data() == c)
    it gives me this error:

    no match for 'operator==' in 'node<T>::data() [with T = Clothing]() == c'

    it does the same thing in the function i wrote for removing residents. Can anyone help me out with this? I don't see why this should be happening since the template class should be returning whatever type of object that is being used in it, then comparing it. sorry if this is unclear but any help would be much appreciated
    -alex

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,702
    The Clothing object needs to define operator==() and operator!=() for wearItem() to compile.

    gg

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    29
    thanks, for some reason I thought I was comparing pointers not objects so I didnt think I had to make my own operators... silly me
    thanks again

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    50
    gg = good game, this is a friggen programming site, you think your elite cause you helped someone, lol if I had of came to this forum earlier i would of said that then improved it, but I wouldn't say gg, cause I'm too good

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 11:17 AM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 09:11 AM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM

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