error C2593: 'operator -' is ambiguous
头文件:
class Coordinate
{
friend Coordinate &operator-(Coordinate &c);
public:
Coordinate();
Coordinate(int x,int y);
Coordinate &operator-();
~Coordinate();
int getX();
void setX(int x);
int getY();
void setY(int y);
private:
int m_iX;
int m_iY;
};
实现类:
#include "Coordinate.h"
#include <iostream>
using namespace std;
Coordinate::Coordinate(int x,int y){
m_iX = x;
m_iY = y;
cout<<"Coordinate() "<<m_iX<<","<<m_iY<<endl;
};
//Coordinate &Coordinate::operator-(){
// this->m_iX=-this->m_iX;
// this->m_iY=-this->m_iY;
// return *this;
// };
Coordinate &operator-(Coordinate &c){
c.m_iX = -c.m_iX;
c.m_iY = -c.m_iY;
return c;
};
Coordinate::Coordinate(){
cout<<"Coordinate() "<<endl;
};
Coordinate::~Coordinate(){
cout<<"~Coordinate() "<<m_iX<<","<<m_iY<<endl;
};
int Coordinate::getX(){
return m_iX;
};
int Coordinate::getY(){
return m_iY;
};
void Coordinate::setX(int x)
{
m_iX = x;
};
void Coordinate::setY(int y)
{
m_iY = y;
};
测试:
#include "Coordinate.h"
#include <iostream>
using namespace std;
int main(void)
{
Coordinate coo(1,3);
cout<<coo.getX()<<","<<coo.getY()<<endl;
-coo;
cout<<coo.getX()<<","<<coo.getY()<<endl;
return 0;
}
结果: