你的第九行代码似乎报错了
时间: 2024-03-27 18:39:41 浏览: 13
非常抱歉,我的代码中第九行确实有问题。应该先声明 Point 类,然后在 Rectangle 类中使用 Point 类。我来修正一下代码:
```cpp
#include <iostream>
#include <cmath>
using namespace std;
class Point; // 先声明 Point 类
class Rectangle {
private:
Point p1, p2; // 矩形的两个顶点
public:
Rectangle(Point p1, Point p2) : p1(p1), p2(p2) {}
double perimeter(); // 周长
double area(); // 面积
};
class Point {
private:
double x, y; // 点的坐标
public:
Point(double x, double y) : x(x), y(y) {}
friend double distance(Point p1, Point p2); // 计算两点之间的距离
friend double Rectangle::perimeter(); // 友元函数:计算周长
friend double Rectangle::area(); // 友元函数:计算面积
};
double distance(Point p1, Point p2) {
return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
}
double Rectangle::perimeter() {
double a = distance(p1, Point(p2.x, p1.y)); // 矩形的一条边长
double b = distance(Point(p2.x, p1.y), p2); // 矩形的另一条边长
return 2 * (a + b); // 周长
}
double Rectangle::area() {
double a = distance(p1, Point(p2.x, p1.y)); // 矩形的一条边长
double b = distance(Point(p2.x, p1.y), p2); // 矩形的另一条边长
return a * b; // 面积
}
int main() {
Point p1(1, 1);
Point p2(4, 5);
Rectangle rect(p1, p2);
cout << "周长:" << rect.perimeter() << endl;
cout << "面积:" << rect.area() << endl;
return 0;
}
```
现在代码应该没有问题了,非常感谢您的提醒!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)