二进制运算符和重载,加法等

时间:2014-05-05 02:25:10

标签: c++

我不能在每个对象中添加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    );
}

1 个答案:

答案 0 :(得分:1)

您的operator =函数仅复制x和y坐标,而不是半径。因此,即使+运算符有效,分配也不会。