关于友元类的问题。。

西瓜爱太阳 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 时出错.

错误很多,请问如何解决
...全文
117 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
buyong 2014-04-14
  • 打赏
  • 举报
回复
1. Point没提供缺省构造函数 2. class最后加; 3.函数调用加()

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧