gpt4 book ai didi

C++ - 未找到运算符

转载 作者:太空宇宙 更新时间:2023-11-04 15:18:31 24 4
gpt4 key购买 nike

我有一个 vector ,其中填充了自定义类型的值,而 find() 算法提示说它找不到合适的 == 运算符来进行值比较。我是这样实现的:

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

我也尝试过“ friend ”方法,但这也不起作用。该类本身的结构如下:

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;

我正在传递一个指针作为比较值...只需取消引用它,find() 现在就可以工作了。

最佳答案

像这样声明你的比较运算符:

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

注意尾随 const

关于C++ - 未找到运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25628555/

24 4 0
文章推荐: c++ - reinterpret_cast 无效转换
文章推荐: twitter-bootstrap - Bootstrap Accordion - 使用 而不是
文章推荐: css - 如何平均分离li
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com