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
   Message(int idnum, char* inmsg);
   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 );

   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!

Edited by ThuggyWuggy: non-specific

6 Years
Discussion Span
Last Post by ThuggyWuggy

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

Edited by gerard4143: n/a

Votes + Comments
Solved problem, thanks

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.


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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.