-3

I'm using QT to design an app and I can't overload == for a class. Equals works perfectly but == doesn't work and I can't figure out why.

Here is my code:

  class connection{
        public:
            connection(State* s, Transition* t);
            connection(Transition* t, State* s);
            State* state;
            Transition* transition;
            bool equals (const connection* c) const; //edited, i missed const in copy paste
            bool operator==(const connection &c) const;
        };
        bool connection::equals(const connection* c) const{ 
            if (state->objectName() == c->state->objectName() && transition->objectName() == c->transition->objectName()){
                return true;
            }
        return false;
        }  

        bool connection::operator==(const connection &c) const{
            if (this->state->objectName() == c.state->objectName() && this->transition->objectName() == c.transition->objectName()){
                return true;
            }
        return false;
    }


// and when i try to compare...
       if (c->equals(d)) // works perfectly
       if (c == d)  // always return false
victor
  • 19
  • 4
  • 2
    When you say id doesn't work, what do you mean? – juanchopanza Aug 25 '15 at 22:21
  • 2
    describe *what* does not work, what are the error messages etc. (BTW: instead of copy pasting code, call equals from operator==) – Hcorg Aug 25 '15 at 22:22
  • 1
    `equals` can't work. You haven't implemented it. – juanchopanza Aug 25 '15 at 22:23
  • 2
    `equals` accepts pointer, `operator==` accepts reference. I hope you're not comparing pointers without dereferencing them. Definition of `equals` also does not match the declaration (you're missing `const` qualifier). – LogicStuff Aug 25 '15 at 22:25
  • there is any compiler error, it just doesn't work as expected. – victor Aug 25 '15 at 23:18
  • 1
    **I hope you're not comparing pointers without dereferencing them** thanks you, that is the problem...sorry for bothering, I've been stuck here for two hours. – victor Aug 25 '15 at 23:31
  • possible duplicate of [Operator overloading](http://stackoverflow.com/questions/4421706/operator-overloading) – MikeMB Aug 26 '15 at 01:00

1 Answers1

1
if(*c == *d) // works perfectly

derefence operator.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
victor
  • 19
  • 4