【问题标题】:C++ - no operator foundC++ - 找不到运算符
【发布时间】:2014-09-02 16:56:13
【问题描述】:

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

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;

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

【问题讨论】:

  • 能否请您出示您的容器并std::find 呼叫它?
  • 您在使用std::vector&lt;const Ship&gt; 吗?上面的代码没问题
  • @Venom 你能显示完整的错误信息吗?

标签: c++ vector operator-overloading


【解决方案1】:

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

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

注意结尾的const

【讨论】:

  • MSVC 仍在返回 C2678。
  • 你能用vectorfind 发布代码吗?
  • @MarcoA。这当然是正确答案的一部分,因为他没有在应该声明的时候声明 operator== const。
  • 我同意这样会更好,但这并不能解决问题,而且根据 clang 也不需要
【解决方案2】:
Ship* potentialShip = ...
std::find(remainingShips.begin(), remainingShips.end(), potentialShip)

你试图找到一个指针,而执行搜索的向量被定义为

std::vector<Ship> remainingShips;

您将指针与 Ship 对象进行比较,因此您的比较是错误的

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

要修复它,要么取消引用指针,要么更改比较函数。

【讨论】:

    【解决方案3】:

    你的

    bool operator==(const Ship& source);
    

    也应该是const,即

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

    但实际上,我更喜欢使用对称运算符,not 作为成员方法。 考虑:

    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);
    }
    

    【讨论】:

    • 更一般地说,我会简单地定义一个成员 isEqual 函数,并从 ComparisonOperators&lt;Ship&gt; 派生,这是一个提供所有比较运算符的模板,来自 isEqualcompare,具体取决于有什么可用的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    • 2023-04-05
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    相关资源
    最近更新 更多