错误代码如下:
#ifndef _Point_h_
#define _Point_h_
#include "stdafx.h"
#include<iostream>
using namespace std;
class point
{
public:
point(double m,double n);
~point(){};
point& operator ++();
point operator ++(int);
point& operator --();
point operator --(int);
friend ostream& operator << (ostream& o,point& p);
private:
double x;
double y;
};
#endif
类的实现:
#include "stdafx.h"
#include"Point.h"
#include<iostream>
#include<ostream>
using namespace std;
//函数的实现
point::point(double m,double n)
{ x=m;
y=n;
}
point& point:: operator ++() //如果返回值不是应用,就不能进行取址运算,只是一个临时变量。++a=5
{
++x;
++y;
return *this;
}
point point:: operator ++(int)
{
point old=(*this);
++(*this);
return old;
}
point& point:: operator --()
{
--x;
--y;
return *this;
}
point point:: operator --(int)
{
point old=(*this);
--(*this);
return old;
}
ostream& point:: operator <<(ostream& o,point& p)
{
.....................
}
注意:frinend 关键字函数 并不是类的成员函数 ,所以在友元函数时不能像成员函数一样用类的::来限定;
所以 要把友元函数的实现改为:
ostream& operator <<(ostream& o,point& p)
{
.....................
}