I attempted to overload the == operator for one of my classes, I even copied a previous implementation of it, but now that I am using pointers it refuses to function properly here is the relevant code (there is a lot so I will provide more if need be)

#ifndef __MESSAGE_H
#define __MESSAGE_H

#include <iostream>
using namespace std;

class Message
{
public:
   Message(int idnum, char* inmsg);
   ~Message();
   Message(const Message* &m);
   Message & operator= ( const Message & assignFrom );
   bool operator== ( const Message* & m ) const;
   bool operator!= ( const Message* & m ) const;
   friend istream & operator>> ( istream is, Message* &m);
   friend ostream & operator<< ( ostream os, const Message* &m );

private:
   int id;        // Message Identifier
   int size;      // Current number of chars allocated for Message
   int count;     // Current number of chars stored in Message
   char * msg;    // Stores the message data
   static const int GROW_BY = 10;
   void Grow();
   
};

and then the implementation in the .cpp file...

bool Message::operator== ( const Message* & m ) const
{
   return id == m->id;
}

and where the problem is...

InfoType * LList::Find ( const InfoType & x ) const
{
   Node* p = list;
   if(p != NULL)
      while(p != NULL)
      {
         if(p->infoPtr != NULL && *p->infoPtr == x)
            return p->infoPtr;
         p = p->next;
      }
   return NULL;
}

Note: I used typedef Message* InfoType and infoPtr is an InfoType* pointer; the issue being where it says *p->infoPtr == x reading: Error 3 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Message' (or there is no acceptable conversion) j:\2630\prog21\llist.cpp 54 1


Thanks for your help!

Why are you using a reference to a pointer in your equals operator?

commented: Solved problem, thanks +1

Should I rewrite it as:

bool Message::operator== ( const Message & m ) const
    {
       return id == m->id;
    }

then? Also, if it is self-assigned, won't it just return true anyway? I want it to return true as long as the id variable is the same, so self-comparison should be true, and I want it to be true.

I would drop all the references to pointers(* &) and just use references like the above posting.

Unfortunately, some of them are required for this program for some reason... However, this did fix the immediate problem. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.