Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   sorted list program (http://www.go4expert.com/forums/sorted-list-program-t25063/)

kris289 25Feb2011 06:38

sorted list program
 
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;   
      }
}


xpi0t0s 26Feb2011 14:38

Re: sorted list program
 
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.

xpi0t0s 26Feb2011 14:41

Re: sorted list program
 
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))
......(.................................................)
.............................(...............==........).
.................................(....)..................



All times are GMT +5.5. The time now is 15:25.