Link to home
Create AccountLog in
Avatar of rround
rround

asked on

Overloading operator==

I have a question about overloading operator== within a template class.  Here is a copy of my code so far.  I just can't seem to get this function to compile without errors.  Eventually, I want to be able to compare the strings held in MyAddress.name while searching the list.  I may be completely off track here so any assistance would be appreciated.

Thanks.


#include<iostream.h>
#include<stdlib.h>

template<class ItemType>
struct NodeType
      {
      ItemType info;
      NodeType *next;
      };


template<class ItemType>
class ListClass
{

public:

      // Constructor and Destructor
      ListClass()
      {
      cout << "Class Constructor Running." << endl;
      length = 0;
      listData = NULL;
      }


      ~ListClass()
      {
      cout << "Class Destructor Running." << endl;
      MakeEmpty();
      }

      
      void MakeEmpty()
      {
      cout << "MakeEmpty() is running" << endl;
      NodeType<ItemType>* tempPtr;
      while (listData != NULL)
            {
            tempPtr = listData;
            listData = listData -> next;
            delete tempPtr;
            }
      length = 0;
      }

      
      bool operator==(ItemType other)
      {
            NodeType<ItemType> temp;

            //return (temp.name, other.name)?false:true;
      }


      void FindItem(ItemType& item, bool& foundIt)
      {
            bool more;
            NodeType<ItemType>* location;

            location = listData;
            foundIt = false;
            more = (location != NULL);

            while (more && !foundIt)
            {
                  if (item == location -> info)
                  {
                        foundIt = true;
                        item = location -> info;
                  }
                  else
                  {
                        location = location -> next;
                        more = (location != NULL);
                  }
            }
      }
            

      bool IsEmpty() const
      {
      return (length == 0);
      }

      
      bool IsFull() const
      {
      NodeType<ItemType>* ptr;
      ptr = new NodeType<ItemType>;
      if (ptr == NULL)
            return true;
      else
            {
            delete ptr;
            return false;
            }
      }

      
      int LengthIs() const
      {
            return length;
      }

      
      void InsertItem(ItemType newItem)
      {
            NodeType<ItemType>* location;

            location = new NodeType<ItemType>;
            location -> info = newItem;
            location -> next = listData;
            listData = location;
            length++;
      }


      void DeleteItem(ItemType item)
      {
            NodeType<ItemType>* location = listData;
            NodeType<ItemType>* tempLocation;

            if (item == listData -> info)
            {
                  tempLocation = location;
                  listData = listData -> next;
            }
            else
            {
                  while (!(item == (location -> next) -> info))
                        location = location -> next;
            
                  tempLocation = location -> next;
                  location -> next = (location -> next) -> next;
            }
            delete tempLocation;
            length--;
            }


private:
      int length;
      NodeType<ItemType>* listData;
      NodeType<ItemType>* currentPosition;

};



#include <fstream.h>

struct Address
{
      char name[50];
      char streetAddress[50];
      char city[50];
      char phone[15];
};

void main()
{

      Address MyAddress;

      ListClass<Address> myList;
      myList.MakeEmpty();

      fstream inData("input.dat", ios::in);

      while (!inData.eof())
      {
      inData.getline(MyAddress.name, 50);
      inData.getline(MyAddress.streetAddress, 50);
      inData.getline(MyAddress.city, 50);
      inData.getline(MyAddress.phone, 15);

      myList.InsertItem(MyAddress);
      }
      
// print contents of list to screen and delete from list
while (!myList.IsEmpty())
      {
      cout << endl;
      cout << MyAddress.name << endl;
      cout << MyAddress.streetAddress << endl;
      cout << MyAddress.city << endl;
      cout << MyAddress.phone << endl;
      myList.DeleteItem(MyAddress);

      }
      cout << endl;
      inData.close();
}
Avatar of nietod
nietod

I'm not sure how you want operator == to work.  i.e what are the conditions under which two ListClass objects are considered equal? You have to figure that out (depending on what your needs are), then its not to hard to write the operator.  I woudl make the following changes.  Both parameters to the operator should be constant so write it like

bool operator==(const ItemType &other) const
{
   // what di you want to test for here?
}
bool operator==(ItemType other)

Try to rewrite it as:

bool operator==(ItemType& other)
....
Sorry ,nietod.I dont see your comment because i'm writing my answer.
I withdraw it and you plz.
Avatar of rround

ASKER

Somehow, I want to pass the char array name from the struct and compare it to a string that the user enters.  So basically I'll be searching the list for a string. If it finds a matching string, the whole struct gets passed back.  This way you can search for an address by using a name.
returning a success/error indicator via a reference parameter like

void FindItem(ItemType& item, bool& foundIt)

is often inconvenient for the code calling the function.  Instead use a return value.  Also since the fucntion doesn;t change the list object, it should be constant, like

bool FindItem(Itemtype &Item) const.

In

bool IsFull() const
{
   NodeType<ItemType>* ptr;
   ptr = new NodeType<ItemType>;
   if (ptr == NULL)
      return true;
   else
   {
      delete ptr;
      return false;
   }
}

What the @$#$% are you doing?  Why are you creating and deleting a node?

In

void InsertItem(ItemType newItem)

you can pass the itemType as a constant reference, like

void InsertItem(const ItemType &newItem)

Same with

void DeleteItem(ItemType item)
>> I want to pass the char array name from the
>> struct and compare it to a string that the user
>> enters.  So basically I'll be searching the list for
>> a string. If it finds a matching string, the whole
>> struct gets passed back.  This way you can
>> search for an address by using a name.
What character array?  A listClass will be on the left side of the operator.  Do you not want a ListClass on the right?  i.e. do you want a string (character array) on the right?
Avatar of rround

ASKER

bool IsFull() const
{
   NodeType<ItemType>* ptr;
   ptr = new NodeType<ItemType>;
   if (ptr == NULL)
      return true;
   else
   {
      delete ptr;
      return false;
   }
}

What the @$#$% are you doing?  Why are you creating and deleting a node?

I took this directly from the book??  It made sense to me, my instructor said we had to delete this stuff.
What is the point of what it is trying to do.  From the function name it sounds like it is to determine if the container is full.  But a linked list container can never be full.  What is more the code is garbage.  it will always return false.  Which is good since a linked list can naver be full, but why go to all that trouble to return false.

the code looks like it may rely on the fact that new returns NULL when it runs out of memory.  But that is not true.  It was true about 6 or 8 years ago, but not any more.
Avatar of rround

ASKER

Maybe I need to make my intentions clearer.  In both FindItem() and DeleteItem() I have an == operation.  The project required the use of a template that's why I have used it.  I declared a struct Address.  Within Address is the character array name, address etc. The struct Address is placed in the and I somehow need to search the list matching the name field of the struct with the user input.  This is kind of confusing to me so I may be completely off track with the way I'm trying to accomplish this.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of AlexVirochovsky
AlexVirochovsky

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
O , i see full discussion. I don't sure, that that code works! But translation, as rround asks, without
errors.
Could very well be what is needed. If you use an operator like == on the type argument of a template, that operator must be defined for that type. As ALex shows.
Avatar of rround

ASKER

It's taken me a while to understand these concepts, but thanks for all your comments and help.