사용자 정의 유형의 값으로 채워진 벡터가 있는데, 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;
나는 비교 값으로 포인터를 전달했다 ... 단순히 그것을 참조를 취소하고 찾을 수는() 작동합니다.
용기 및'std :: find' 호출을 표시해주십시오. – P0W
'std :: vector'을 사용하고 있습니까? 위의 코드는 괜찮습니다. –
@Venom 전체 오류 메시지를 표시 할 수 있습니까? –