如何解决此错误,其原因是什么?

时间:2019-09-18 04:56:13

标签: c++

我得到一个错误,当我单击查看错误时,我将我带到一个名为xutility的文档。

这是两个错误:

  1. 错误C2064项的求值结果不为带有2个参数的函数。 Studio \ 2019 \ Community \ VC \ Tools \ MSVC \ 14.21.27702 \ include \ xutility 624

2。错误C2056非法表达式Funtwo C:\ Program Files(x86)\ Microsoft Visual Studio \ 2019 \ Community \ VC \ Tools \ MSVC \ 14.21.27702 \ include \ xutility 624

然后,当我查看第624行时,它向我显示了

// FUNCTION TEMPLATE _Debug_lt_pred
template <class _Pr, class _Ty1, class _Ty2>
constexpr bool _Debug_lt_pred(_Pr&& _Pred, _Ty1&& _Left, _Ty2&& _Right) _NOEXCEPT_COND(
    noexcept(_Pred(_Left, _Right))
    && noexcept(_Pred(_Right, _Left))) { // test if _Pred(_Left, _Right) and _Pred is strict weak ordering
    const auto _Result = static_cast<bool>(_Pred(_Left, _Right));
    if (_Result) {
        _STL_VERIFY(!_Pred(_Right, _Left), "invalid comparator");
    }

    return _Result;
}

我的课:

class Vector3D : public Vector2
{
public:

    Vector3D();
    ~Vector3D();
    double GetMagnitude();
    void Normalize();
    void scale(Vector3D vector);
    void scale(double vector3x, double vector3y, double vector3z);

    Vector3D operator -();
    Vector3D operator +(Vector3D other);
    Vector3D operator -(Vector3D other);
    Vector3D operator *(Vector3D other);
    Vector3D operator /(Vector3D other);

    bool operator < (Vector3D other);
    bool operator > (Vector3D other);
    bool operator <= (Vector3D other);
    bool operator >= (Vector3D other);
    bool operator == (Vector3D other);
    bool operator != (Vector3D other);

    void operator = (Vector3D other);
    void operator += (Vector3D other);
    void operator -= (Vector3D other);
    void operator *= (Vector3D other);
    void operator /= (Vector3D other);

    friend ostream& operator <<(ostream& out, const Vector3D& v);
    friend istream& operator >>(istream& in, Vector3D& v);

    double m_z = 0;
};

<运营商实施

bool Vector3D::operator<(Vector3D other)
{
    if (GetMagnitude() < other.GetMagnitude())
        return true;
    return false;
}

对于每个成员的实施:

Vector3D::Vector3D()
{
}

Vector3D::~Vector3D()
{
}

double Vector3D::GetMagnitude()
{
    return sqrt(pow(m_x, 2) + pow(m_y, 2) + pow(m_z, 2));
}

void Vector3D::Normalize()
{
    double maxValue = std::max(m_x, m_y, m_z);
    m_x = m_x / maxValue;
    m_y = m_y / maxValue;
    m_z = m_z / maxValue;
}

void Vector3D::scale(Vector3D vector)
{
    m_x *= vector.m_x;
    m_y *= vector.m_y;
    m_z *= vector.m_z;
}

void Vector3D::scale(double vector3x, double vector3y, double vector3z)
{
    m_x *= vector3x;
    m_y *= vector3y;
    m_z *= vector3z;
}

Vector3D Vector3D::operator-()
{
    Vector3D v;
    v.m_x = -m_x;
    v.m_y = -m_y;
    v.m_z = -m_z;
    return v;
}

Vector3D Vector3D::operator+(Vector3D other)
{
    Vector3D v;
    v.m_x = m_x + other.m_x;
    v.m_y = m_y + other.m_y;
    v.m_z = m_z + other.m_z;
    return v;
}

Vector3D Vector3D::operator-(Vector3D other)
{
    Vector3D v;
    v.m_x = m_x - other.m_x;
    v.m_y = m_y - other.m_y;
    v.m_z = m_z - other.m_z;
    return v;
}

Vector3D Vector3D::operator*(Vector3D other)
{
    Vector3D v;
    v.m_x = m_x * other.m_x;
    v.m_y = m_y * other.m_y;
    v.m_z = m_z * other.m_z;
    return v;
}

Vector3D Vector3D::operator/(Vector3D other)
{
    Vector3D v;
    v.m_x = m_x / other.m_x;
    v.m_y = m_y / other.m_y;
    v.m_z = m_z / other.m_z;
    return v;
}

bool Vector3D::operator<(Vector3D other)
{
    if (GetMagnitude() < other.GetMagnitude())
        return true;
    return false;
}

bool Vector3D::operator>(Vector3D other)
{
    if (GetMagnitude() > other.GetMagnitude())
        return true;
    return false;
}

bool Vector3D::operator<=(Vector3D other)
{
    if (GetMagnitude() <= other.GetMagnitude())
        return true;
    return false;
}

bool Vector3D::operator>=(Vector3D other)
{
    if (GetMagnitude() >= other.GetMagnitude())
        return true;
    return false;
}

bool Vector3D::operator==(Vector3D other)
{
    if (GetMagnitude() == other.GetMagnitude())
        return true;
    return false;
}

bool Vector3D::operator!=(Vector3D other)
{
    if (GetMagnitude() != other.GetMagnitude())
        return true;
    return false;
}

void Vector3D::operator=(Vector3D other)
{
    m_x = other.m_x;
    m_y = other.m_y;
    m_z = other.m_z;
}

void Vector3D::operator+=(Vector3D other)
{
    m_x += other.m_x;
    m_y += other.m_y;
    m_z += other.m_z;
}

void Vector3D::operator-=(Vector3D other)
{
    m_x -= other.m_x;
    m_y -= other.m_y;
    m_z -= other.m_z;
}

void Vector3D::operator*=(Vector3D other)
{
    m_x *= other.m_x;
    m_y *= other.m_y;
    m_z *= other.m_z;
}

void Vector3D::operator/=(Vector3D other)
{
    m_x /= other.m_x;
    m_y /= other.m_y;
    m_z /= other.m_z;
}

ostream& operator<<(ostream& out, const Vector3D& v)
{
    out << v.m_x << ", " << v.m_y << ", " << v.m_z;
    return out;
}

istream& operator>>(istream& in, Vector3D& v)
{
    cout << "Vector 2 Input" << endl << "x:";
    in >> v.m_x;

    cout << "y : ";
    in >> v.m_y;

    cout << "z : ";
    in >> v.m_z;

    return in;
}

我只是从类向量2继承两个成员。它们是x和y。 m_x和m_y。

1 个答案:

答案 0 :(得分:3)

您正在尝试更改操作员的行为。不要那样做!常量正确性不是可选的wrt运算符。这些是您错误声明的运算符(以及应如何声明)。

    Vector3D operator -() const;
    Vector3D operator +(const Vector3D& other) const;
    Vector3D operator -(const Vector3D& other) const;
    Vector3D operator *(const Vector3D& other) const;
    Vector3D operator /(const Vector3D& other) const;

    bool operator < (const Vector3D& other) const;
    bool operator > (const Vector3D& other) const;
    bool operator <= (const Vector3D& other) const;
    bool operator >= (const Vector3D& other) const;
    bool operator == (const Vector3D& other) const;
    bool operator != (const Vector3D& other) const;

    // non-const methods, that return mutable reference to this. 
    Vector3D& operator = (const Vector3D& other);
    Vector3D& operator += (const Vector3D& other);
    Vector3D& operator -= (const Vector3D& other);
    Vector3D& operator *= (const Vector3D& other);
    Vector3D& operator /= (const Vector3D& other);

我假设错误是因为它试图在一对向量上调用operator <,但是它们都是const,所以没有现有的operator <重载可以对值进行处理(因为它们都是const) 。