2013-11-22 2 views
1

내가 unordered_set에 요소를 삽입하려고 할 때이 오류를 받고 있어요에 유효하지 않은 피연산자 : 여기unordered_set : 이진 표현 ('const를 플레이'와 'const를 플레이')

error: invalid operands to binary expression ('const Play' and 'const Play') 
     {return __x == __y;} 

은의 스크린 샷입니다 전체 오류 :이 내 해시 함수입니다 https://www.dropbox.com/s/nxq5skjm5mvzav3/Screenshot%202013-11-21%2020.11.24.png

: 나는이 unordered_list를 선언 곳

struct Hash { 

     size_t operator() (Play &play) { 

      unsigned long hash = 5381; 
      int c; 

      string s_str = play.get_defense_name() + play.get_offense_name() + play.get_description(); 
      const char * str = s_str.c_str(); 

      while ((c = *str++)) 
       hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ 

      cout << hash << endl; 
      return hash; 

     } 
    }; 

입니다 :

unordered_set<Play, Hash> Plays; 

그리고이 내 내 Play 클래스 == 오버로드 : 여기에 갈 수 있는지

friend bool operator== (Play& p1, Play& p2) 
    { 
     return 
      (p1.get_defense_name() == p2.get_defense_name()) && 
      (p1.get_offense_name() == p2.get_offense_name()) && 
      (p1.get_description() == p2.get_description()); 
    } 

어떤 생각을?

감사합니다.

+1

이'연산자의 서명을 변경하려고 ==''const'를 사용하는 CONST 다움을 추가하면 문제를 해결해야한다. 마찬가지로,'친구 bool 연산자 == (const Play & p1, const Play & p2)'. 논리 연산자가 비교 대상을 절대로 변경하지 않는 것이 좋습니다. –

답변

4

오류는 const Playconst Play을 비교하려고 말하고 있지만, 당신은 단지 Play

friend bool operator== (const Play& p1, const Play& p2) 
    { 
     return 
      (p1.get_defense_name() == p2.get_defense_name()) && 
      (p1.get_offense_name() == p2.get_offense_name()) && 
      (p1.get_description() == p2.get_description()); 
    } 
2

그것은 정렬되지 않은 설정과 같은 위해 operator ==을 제공 상수 Play 개체를 비교하기 위해 찾고 있지만 == 이벤트는 비 비교 const 것들. const 요소를 삭제할 수 없기 때문에 컴파일러에서 오류가 발생합니다.

friend bool operator== (const Play& p1, const Play& p2) 

당신은뿐만 아니라 당신의 operator()const을 추가해야합니다 :

size_t operator() (const Play &play)