关于友元类的问题。。
西瓜爱太阳 2014-04-07 03:06:32 初学者基本什么都不懂
不要见笑。。
#include<iostream.h>
#include<math.h>
class Point;
class Distance
{ public:
Distance(double a,double b)
{ x=a ; y=b ;}
double showPoint_Distance(Point &p);
private:
double x;
double y;
}
class Point
{
public:
Point(double a,double b)
{
x=a ; y=b ;
}
double read_x(void);
double read_y(void);
friend Distance;
private:
double x;
double y;
}
double Point::read_x(void)
{ return x;}
double Point::read_y(void)
{ return y;}
double Distance::showPoint_Distance(Point &p)
{
double dx,dy,dis;
dx=p.read_x-x;dy=p.read_y-y;
dis=sqrt(dx*dx+dy*dy);
return dis;
}
void main()
{
Point x1,y1;
Distance x2,y2;
x2.showPoint_Distance(x1);
y2.showPoint_Distance(y1);
cout<<"两点间的距离为"<<dis<<endl;
}
I:\C++\面向对象\exp_308.cpp(14) : error C2236: unexpected 'class' 'Point'
I:\C++\面向对象\exp_308.cpp(14) : error C2143: syntax error : missing ';' before '{'
I:\C++\面向对象\exp_308.cpp(14) : error C2447: missing function header (old-style formal list?)
I:\C++\面向对象\exp_308.cpp(27) : error C2027: use of undefined type 'Point'
I:\C++\面向对象\exp_308.cpp(14) : see declaration of 'Point'
I:\C++\面向对象\exp_308.cpp(28) : error C2065: 'x' : undeclared identifier
I:\C++\面向对象\exp_308.cpp(29) : error C2027: use of undefined type 'Point'
I:\C++\面向对象\exp_308.cpp(14) : see declaration of 'Point'
I:\C++\面向对象\exp_308.cpp(30) : error C2065: 'y' : undeclared identifier
I:\C++\面向对象\exp_308.cpp(34) : error C2027: use of undefined type 'Point'
I:\C++\面向对象\exp_308.cpp(14) : see declaration of 'Point'
I:\C++\面向对象\exp_308.cpp(34) : error C2228: left of '.read_x' must have class/struct/union type
I:\C++\面向对象\exp_308.cpp(34) : error C2027: use of undefined type 'Point'
I:\C++\面向对象\exp_308.cpp(14) : see declaration of 'Point'
I:\C++\面向对象\exp_308.cpp(34) : error C2228: left of '.read_y' must have class/struct/union type
I:\C++\面向对象\exp_308.cpp(40) : error C2079: 'x1' uses undefined class 'Point'
I:\C++\面向对象\exp_308.cpp(40) : error C2079: 'y1' uses undefined class 'Point'
I:\C++\面向对象\exp_308.cpp(41) : error C2512: 'Distance' : no appropriate default constructor available
I:\C++\面向对象\exp_308.cpp(41) : error C2512: 'Distance' : no appropriate default constructor available
I:\C++\面向对象\exp_308.cpp(42) : error C2664: 'showPoint_Distance' : cannot convert parameter 1 from 'int' to 'class Point &'
A reference that is not to 'const' cannot be bound to a non-lvalue
I:\C++\面向对象\exp_308.cpp(43) : error C2664: 'showPoint_Distance' : cannot convert parameter 1 from 'int' to 'class Point &'
A reference that is not to 'const' cannot be bound to a non-lvalue
I:\C++\面向对象\exp_308.cpp(44) : error C2059: syntax error : '<<'
执行 cl.exe 时出错.
错误很多,请问如何解决