| 
 | 
 
10鱼币 
- #include<iostream>
 
 - #include<math.h>
 
 - #define PI 3.14
 
 - using namespace std;
 
 - class point
 
 - {
 
 - public:
 
 -  double distance(int c,int d);
 
 - };
 
 - class Circle
 
 - {
 
 - public:
 
 -  int r,s,a,b;
 
 -  Circle();
 
 -  ~Circle();
 
 -  void cla();
 
 -  void location(int c,int d);
 
 -  friend double point::distance(int c,int d);
 
  
-  
 
 - };
 
  
- Circle::Circle ()
 
 - { 
 
 -  r=3;
 
 -  a=0;
 
 -  b=0;
 
 -  cout<<"该圆的坐标为"<<a<<","<<b<<"\n"<<endl;
 
 -  cout<<"该圆的半径为"<<r<<endl;
 
 - }
 
 - Circle::~Circle(){}
 
 - double point::distance(int c,int d)
 
 - {
 
 -  int p;
 
 -  p=sqrt((c-a)*(c-a)+(d-b)*(d-b));
 
 -  return p;
 
 - }
 
 - void Circle::cla()
 
  
- {
 
 -  s=2*PI*r*r; 
 
 -  cout<<"该圆的面积为"<<s<<"\n"<<endl;
 
 - }
 
 - void Circle::location(int c,int d)
 
  
- {
 
 -  int L;
 
 -  L=(a-c)*(a-c)+(b-d)*(b-d);
 
 -  if(L>r*r)
 
 -  {
 
 -   std::cout<<"该点位于圆外\n"<<endl;
 
 -  }
 
 -  if(L<r*r)
 
 -  {
 
 -   std::cout<<"该点位于圆内\n"<<endl;
 
 -  }
 
 -  if(L=r*r)
 
 -  {
 
 -   std::cout<<"该点位于圆上\n"<<endl;
 
 -  }
 
 - }
 
 - int main()
 
 - {
 
 -  int c,d,m;
 
 -  cout<<"input two point"<<endl;
 
 -  cin>>c>>d;
 
 -  Circle cir1;
 
 -  cir1.cla();
 
 -  cir1.location(c,d);
 
 -  m=distance(c,d);
 
 -  cout<<"该点到圆心的距离为:"<<m;
 
 -  return 0;
 
 -  
 
 - }
 
  复制代码 
point这个函数是circle类的友元函数,a,b在circle内设为了public的,但point还是调用不了circle中的a,b,为啥子?该怎么改才能调用?问了周围两三个同学都没找出原因,求大佬帮助 
图片中有错误的地方 
首先,point::distance函数应该定义为static,因为distance()函数必须通过point类的实例对象才能访问 
其次,你在distance()函数内调用a和b干什么?它们是Circle类的成员变量,要通过Circle类的对象才能访问,直接访问当然不行了 
 
 
 |   
 
 
最佳答案
查看完整内容 
首先,point::distance函数应该定义为static,因为distance()函数必须通过point类的实例对象才能访问
其次,你在distance()函数内调用a和b干什么?它们是Circle类的成员变量,要通过Circle类的对象才能访问,直接访问当然不行了 
 
 
 
 
 
 
 |