我该如何解决这个错误,它的原因是啥?

Posted

技术标签:

【中文标题】我该如何解决这个错误,它的原因是啥?【英文标题】:How can I fix This error and what is the cause of it?我该如何解决这个错误,它的原因是什么? 【发布时间】:2019-09-18 04:56:13 【问题描述】:

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

这是两个错误:

    错误 C2064 术语不计算为采用 2 个参数的函数。 Studio\2019\Community\VC\Tools\MSVC\14.21.27702\include\xutility 624

2.Error 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。

【问题讨论】:

您是否已发布所有您的代码?你的要求在哪里?实施在哪里? 您没有遵循有关const-ness 对象的运算符重载的通常准则。见***.com/questions/4421706/…。 @Water 上传了。 @Mario 这也是一切吗?您在哪里实例化和使用该类? 还有个小建议:可以把这个转成单行:bool Vector3D::operator&lt;(Vector3D other) return GetMagnitude() &lt; other.GetMagnitude(); 【参考方案1】:

您正试图改变操作员的行为。不要那样做! const 正确性不是可选的 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

【讨论】:

我也将 getmagnitude 更改为 getmagnitude()const 但我仍然遇到相同的错误

以上是关于我该如何解决这个错误,它的原因是啥?的主要内容,如果未能解决你的问题,请参考以下文章

是啥导致错误“无法访问 Foo 类型的封闭实例”,我该如何解决?

我出现内存不足错误,如何解决?Permgen 空间区域是啥意思?是啥原因造成的? [复制]

Ruby on Rails:Xcode:仪器:跟踪/BPT 陷阱?它是啥,我该如何解决?

网页提示错误代码405。是啥意思?如何解决?求高人指点。。

什么是 NullReferenceException,我该如何解决?

什么是 NullReferenceException,我该如何解决?