gpt4 book ai didi

c++ - 二元运算符和重载、加法等

转载 作者:行者123 更新时间:2023-11-28 06:53:28 25 4
gpt4 key购买 nike

我不能在每个对象中加在一起超过 2 个东西。例如。我有一个 xCord、一个 yCord、一个半径、一个周长和一个面积。当我将对象加在一起时,只有 xCords 和 yCords 加在一起。其余的保持原样。

circleT.h 文件,我的编译器也较旧,因此由于某种原因它无法处理 cpp 中的模板。所以如果您想知道的话,一切都在头文件中。

//  MATHEMATICAL OPERATORS

// BINARY operators
template<typename T>
CircleT<T> CircleT<T>::operator+(const CircleT<T>& rhs)
{
CircleT temp;
CircleT tempC;
temp.xCord = this->xCord + rhs.xCord;
temp.yCord = this->yCord + rhs.yCord;
temp.radius = this->radius + rhs.radius;
return temp;
}

template<typename T>
CircleT<T> CircleT<T>::operator-(const CircleT<T>& rhs)
{
CircleT temp;
temp.xCord = this->xCord - rhs.xCord;
temp.yCord = this->yCord - rhs.yCord;
return temp;
}
template<typename T>
CircleT<T> CircleT<T>::operator*(const CircleT<T>& rhs)
{
CircleT temp;
temp.xCord = this->xCord * rhs.xCord;
temp.yCord = this->yCord * rhs.yCord;
return temp;
}
template<typename T>
CircleT<T> CircleT<T>::operator/(const CircleT<T>& rhs)
{
CircleT temp;
temp.xCord = this->xCord / rhs.xCord;
temp.yCord = this->yCord / rhs.yCord;
return temp;
}


// UNARY operators
template<typename T>
CircleT<T>& CircleT<T>::operator+=(const CircleT<T>& rhs)
{
this->xCord += rhs.xCord;
this->yCord += rhs.yCord;
return *this;
}
template<typename T>
CircleT<T>& CircleT<T>::operator-=(const CircleT<T>& rhs)
{
this->xCord -= rhs.xCord;
this->yCord -= rhs.yCord;
return *this;
}
template<typename T>
CircleT<T>& CircleT<T>::operator*=(const CircleT<T>& rhs)
{
this->xCord *= rhs.xCord;
this->yCord *= rhs.yCord;
return *this;
}
template<typename T>
CircleT<T>& CircleT<T>::operator/=(const CircleT<T>& rhs)
{
this->xCord /= rhs.xCord;
this->yCord /= rhs.yCord;
return *this;
}

template<typename T>
CircleT<T>& CircleT<T>::operator=(const CircleT<T>& rhs)
{
this->xCord = rhs.xCord;
this->yCord = rhs.yCord;
return *this;
}

template<typename T>
CircleT<T> CircleT<T>::operator++(int ignoreThis)
{
// this is the ** POST ** increment
double tempxCord = xCord;
double tempyCord = yCord;
xCord++;
yCord++;
return CircleT();
}
template<typename T>
CircleT<T> CircleT<T>::operator++()
{
// this is the ** PRE ** increment
xCord++;
yCord++;
return CircleT();
}



template<typename T>
CircleT<T> CircleT<T>::operator--(int ignoreThis)
{
// this is the ** POST ** increment
double tempxCord = xCord;
double tempyCord = yCord;
xCord--;
yCord--;
return CircleT();
}
template<typename T>
CircleT<T> CircleT<T>::operator--()
{
// this is the ** PRE ** increment
xCord--;
yCord--;
return CircleT();
}




// LOGICAL operators
template<typename T>
bool CircleT<T>::operator==(const CircleT<T>& rhs)
{
return ( this->xCord == rhs.xCord && this->yCord == rhs.yCord );
}
template<typename T>
bool CircleT<T>::operator!=(const CircleT<T>& rhs)
{
return ( this->xCord != rhs.xCord && this->yCord != rhs.yCord );
}
template<typename T>
bool CircleT<T>::operator<(const CircleT<T>& rhs)
{
double areaL = this->xCord * this->yCord;
double areaR = rhs.xCord * rhs.yCord;
return ( areaL < areaR );
}
template<typename T>
bool CircleT<T>::operator<=(const CircleT<T>& rhs)
{
double areaL = this->xCord * this->yCord;
double areaR = rhs.xCord * rhs.yCord;
return ( areaL <= areaR );
}
template<typename T>
bool CircleT<T>::operator>(const CircleT<T>& rhs)
{
double areaL = this->xCord * this->yCord;
double areaR = rhs.xCord * rhs.yCord;
return ( areaL > areaR );
}
template<typename T>
bool CircleT<T>::operator>=(const CircleT<T>& rhs)
{
double areaL = this->xCord * this->yCord;
double areaR = rhs.xCord * rhs.yCord;
return ( areaL >= areaR );
}

最佳答案

您的 operator = 函数只复制 x 和 y 坐标,不复制半径。因此,即使 + 运算符起作用,赋值也不起作用。

关于c++ - 二元运算符和重载、加法等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23463890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com