sorted list program

Discussion in 'C' started by kris289, Feb 25, 2011.

  1. kris289

    kris289 New Member

    Joined:
    Feb 25, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I need help with the program. My line of code
    while (ptr != NULL && compare(x, (*ptr).data == GREATER))
    registers an error that says
    lab1.cpp no match for 'operator==' in 'ptr->node::data == GREATER'
    I use dev c++ as my compiler
    any help will be appreciated

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    
    
                  struct itemType
                  {
                         string author;
                         string title;
                         float price;
                  };
                  
                  struct node
                  {
                   itemType data;
                  node *next;
                  };
                  
                  
    class sortedList
    {
          private:
                  node *head;
                  
                  
          public: 
                  void add (itemType x);  
                  sortedList ();  
    };
    
    
    
    enum compType {LESSER, GREATER, EQUAL};
    
    compType compare(itemType x, itemType y);
    
    int main ()
    {
        typedef itemType book;
        sortedList shelf;
        book a,b,c,d;
    
        a.author = "J.K Rowling";
        a.title = "Harry Potter";
        a.price = 19.99;
        
        b.author = "C.S Lewis";
        b.title = "Chronicles of Narnia";
        b.price = 9.99;
        
        c.author = "Darren Shan";
        c.title = "Procession of the Dead";
        c.price = 14.99;
        
        d.author = "Albert Einstein";
        d.title = "Theory of Relativity";
        d.price = 4.99;
        
        shelf.add(a);
        shelf.add(b);
        shelf.add(c);
        shelf.add(d);
        
        cin.get();
        return 0;   
    }
    
    sortedList :: sortedList ()
    {
         head = NULL;    
    }
    
    void sortedList :: add (itemType x)
    {
        node *prev = NULL;
        node *ptr = NULL;
        node *temp = NULL;
        
        prev = NULL;
        ptr = head;
         
               while (ptr != NULL && compare(x, (*ptr).data == GREATER))
    
               {
                   prev = ptr;
                   ptr = (*ptr).next;
               }
               
               temp = new node;
               (*temp).data = x;
               
               if (prev == NULL)
               {
                  (*temp).next = head;
    		      head = temp;      
               }
               else
    	{
    		(*temp).next = ptr;
    		(*prev).next = temp;
    	}
    
    }
    
    compType compare (itemType x, itemType y)
    {
          if ( x.price > y.price)
          {
             return GREATER;
          }
          else if (x.price < y.price)
          {
               return LESSER;
          }     
          else 
          {
          return EQUAL;     
          }
    }
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    That's because ptr->data is of type
    Code:
    struct itemType
    {
        string author;
        string title;
        float price;
    };
    
    but GREATER is an enum, i.e. an int. There is no built-in == operator for comparing an int with your structure, so you get the error.
     
    shabbir likes this.
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Also the error could have something to do with brackets. Look more closely at the brackets, and use an editor that shows which brackets match which, so you'll be less likely to make this kind of error:
    Code:
    while (ptr != NULL && compare(x, (*ptr).data == GREATER))
    ......(.................................................)
    .............................(...............==........).
    .................................(....)..................
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice