2014-09-02 3 views
0

사용자 정의 유형의 값으로 채워진 벡터가 있는데, find() 알고리즘은 값 비교에 적합한 == 연산자를 찾을 수 없다는 불만을 제기합니다. 나는 이런 식으로 구현했습니다C++ - 연산자가 없습니다.

bool Ship::operator==(const Ship& source) { 
    return (_type == source._type && 
      _damagedSquares == source._damagedSquares && 
      _orientation == source._orientation && _state == source._state); 
} 

나는 또한 "친구"방법의 접근 방식을 시도했지만 그 중 하나가 작동하지 않습니다. 클래스 자체는 다음과 같이 구성되어있다 :

class Ship { 
private: 
    ShipType _type; 
    int _damagedSquares; 
    ShipOrientation _orientation; 
    ShipState _state; 

public: 
    Ship(); 
    Ship(ShipType type); 
    ~Ship(); 

    bool operator==(const Ship& source); 
}; 

은 내가 잘못 여기서 뭐하는 거지?

추가 정보 :

std::vector<Ship> remainingShips; 
MultiArray& squares = opponentGridCopy.GetSquares(); 
for (RowIterator rowIterator = squares.begin(); rowIterator != squares.end(); 
    ++rowIterator) { 
    for (ColumnIterator columnIterator = rowIterator->begin(); 
     columnIterator != rowIterator->end(); ++columnIterator) { 
     Square* current = &(*columnIterator); 
     SquareState currentState = current->GetState(); 
     if (currentState != SquareState::Hit) 
      current->SetState(SquareState::Vacant); 
     Ship* potentialShip = current->GetOwner(); 
     if (potentialShip != nullptr) { 
      int damagedSquares = potentialShip->GetDamagedSquares(); 
      if (!damagedSquares) { 
       current->SetState(SquareState::Populated); 
       break; 
      } 
      if (remainingShips.empty() || 
       std::find(remainingShips.begin(), remainingShips.end(), 
          potentialShip) == 
        remainingShips.end()) // should be *potentialShip 
       remainingShips.push_back(*potentialShip); 
     } 
    } 
} 
return remainingShips; 

나는 비교 값으로 포인터를 전달했다 ... 단순히 그것을 참조를 취소하고 찾을 수는() 작동합니다.

+0

용기 및'std :: find' 호출을 표시해주십시오. – P0W

+1

'std :: vector '을 사용하고 있습니까? 위의 코드는 괜찮습니다. –

+0

@Venom 전체 오류 메시지를 표시 할 수 있습니까? –

답변

1
Ship* potentialShip = ... 
std::find(remainingShips.begin(), remainingShips.end(), potentialShip) 

당신이 포인터를 찾기 위해 노력하고 검색이

std::vector<Ship> remainingShips; 

당신이 선박 객체에 대한 포인터를 비교하고 있으며 따라서 귀하의 비교가 잘못 정의되어 수행되는 벡터 동안

bool Ship::operator==(const Ship& source) // Accepts a Ship reference, not a pointer 

수정하려면 포인터를 역 참조하거나 비교 기능을 변경하십시오.

+0

그래, 나도 알아 챘다. 그럼에도 불구하고, 고마워. – Venom

5

지금처럼 비교 연산자를 선언 :

bool Ship::operator==(const Ship &source) const 

참고는 const 후행.

+2

MSVC는 여전히 C2678을 반환하고 있습니다. – Venom

+0

'vector'와'find'를 사용하여 코드를 게시 할 수 있습니까? – doctorlove

+1

이것은 ** ** 정확한 대답이 아닙니다 ** –

1
뿐만 아니라

귀하의

bool operator==(const Ship& source); 

이어야한다 CONST, 즉

bool operator==(const Ship& source) const; 

그러나 실제로, 나는 대칭 연산자를 선호, 멤버 방법으로하지. 다음을 고려하십시오.

Class Ship 
{ 
private: 
    ShipType _type; 
    int _damagedSquares; 
    ShipOrientation _orientation; 
    ShipState _state; 

public: 
    Ship(); 
    Ship(ShipType type); 
    ~Ship(); 

    static bool eq(const Ship& s0, const Ship& s1) 
    { 
     return (s0._type == s1._type && 
     s0.damagedSquares == s1._damagedSquares && 
     s0._orientation == s1._orientation && 
     s0._state == s1._state); 
    } 

}; 

inline bool operator==(const Ship& s0, const Ship& s1) 
{ 
    return Ship::eq(s0, s1); 
} 
+0

좀 더 일반적으로 간단히 말해서 멤버 isEqual 함수를 정의하고 사용 가능한 것에 따라 비교 연산자 인 isEqual 또는 compare를 제공하는 템플릿 인 ComparisonOperators 에서 파생됩니다. –

관련 문제