2015-08-25 117 views
-3

我正在使用QT來設計一個應用程序,並且我無法爲某個類重載==。 等於完美,但==不起作用,我不明白爲什麼。如何重載==運算符?

這裏是我的代碼:

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 
+2

當你說的id是不行的,你什麼意思? – juanchopanza

+2

描述*什麼*不起作用,錯誤信息是什麼等等(順便說一下:不是複製粘貼代碼,而是從操作符==調用equals) – Hcorg

+1

'equals'不能工作。你還沒有實現它。 – juanchopanza

回答

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

反引用操作... 我爲那個愚蠢的問題抱歉