#include<iostream> #include<cmath> using namespace std; class Points{ public: Points(double xx=0.0,double yy=0.0); // 用参数初始化点的坐标 double GetX()const; // 返回横坐标 double GetY()const; // 返回纵坐标 friend void print(Points*); // 输出点的坐标 friend double distance(Points,Points); // 求两点间的距离 private: double x,y; // 点的坐标 }; Points::Points(double xx,double yy):x(xx),y(yy) { } double Points::GetX( )const { return x; } double Points::GetY()const { return y; } //ERROR**********found********** friend void print(Points*p) { //ERROR**********found********** cout<<"点:("<<x<<','<<y<<")\n"; } //ERROR**********found********** double Points::distance(Points p1,Points p2) { double dx,dy; dx=p1.x-p2.x; dy=p1.y-p2.y; return sqrt(dx*dx+dy*dy); } int main() { Points pa,pb(1.0,2.0); print(&pa); print(&pb); //ERROR**********found********** cout<<"距离:"<<pa.distance(pa,pb)<<endl; system("pause"); return 0; }错在哪
时间: 2023-07-06 10:42:01 浏览: 47
这段程序有三个错误:
1. 在print函数中输出点的坐标时,应该使用传入的指针p来获取点的坐标,而不是直接使用成员变量x和y。正确的输出语句应该是:
```
cout<<"点:("<<p->x<<','<<p->y<<")\n";
```
2. 在print函数前应该加上Points类的作用域限定符,以表示print是Points类的友元函数。正确的语句应该是:
```
void Points::print(Points*p) {
```
3. 在main函数中调用pa的distance成员函数时,应该使用类名作为限定符,以表示distance是Points类的成员函数。正确的调用语句应该是:
```
cout<<"距离:"<<Points::distance(pa,pb)<<endl;
```
相关问题
#include <iostream> #include<algorithm> #include<cmath> #include <queue> using namespace std;
#include <iostream>:这是C++标准库中的头文件,用于输入输出流操作,包括cin、cout等。
#include<algorithm>:这是C++标准库中的头文件,用于提供各种算法操作,如排序、查找等。
#include<cmath>:这是C++标准库中的头文件,用于提供数学函数操作,如绝对值、平方根等。
#include <queue>:这是C++标准库中的头文件,用于提供队列操作,包括入队、出队等。
using namespace std;:这是C++的命名空间声明,表示使用std命名空间,可以直接使用std中的函数和对象。
#include <iostream> #include <string> #include <stack> #include <vector> #include <iomanip> using namespace std; 逐行注释以上代码
这是一段C++的程序代码,使用了几个标准库的头文件。以下是逐行的注释:
```
#include <iostream> // 输入输出流库头文件
#include <string> // 字符串库头文件
#include <stack> // 栈库头文件
#include <vector> // 向量库头文件
#include <iomanip> // 格式控制库头文件
using namespace std; // 命名空间
```
这段程序代码导入了五个标准库的头文件,分别是iostream、string、stack、vector、iomanip,它们分别用于处理输入输出、字符串、栈、向量和格式控制。其中iostream是C++中最常用的输入输出流库,string是用于字符串操作的库,stack是用于栈数据结构的库,vector是用于动态数组的库,iomanip是用于格式化输出的库。
最后一行代码使用了命名空间std,这意味着所有的标准库函数和变量都在std命名空间中。如果不使用using namespace std,则需要在使用标准库函数时加上std::前缀。
相关推荐
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)