![](/img/trans.png)
[英]What is the cause of a segmentation fault error (signal name: SIGSEGV), and how would I find/fix it?
[英]How can I fix This error and what is the cause of it?
我收到一個錯誤,當我單擊查看錯誤時,我將我帶到一個名為xutility的文檔中。
這是兩個錯誤:
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。
您正在嘗試更改操作員的行為。 不要那樣做! 常量正確性不是可選的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)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.