/workspace/CppProject/main.cpp:6:12: note: declared private here float x,y; ^ 2 errors generated.
时间: 2024-02-02 20:03:05 浏览: 28
这个错误提示表明,您在类 Point 中声明的成员变量 x 和 y 是私有的,不能在类外直接访问。而在 main 函数中,您尝试使用 cin 来输入这些私有成员变量,因此编译器报错。
为了解决这个问题,您可以提供一个公有的成员函数来设置私有成员变量。例如,您可以添加以下代码:
```
class Point {
private:
float x,y;
public:
void setPoint(float x, float y) {
this->x = x;
this->y = y;
}
double suan() {
return sqrt(x*x+y*y);
}
};
int main() {
Point p[5];
double dis[5];
for(int i=0;i<5;i++){
float x, y;
cin >> x >> y;
p[i].setPoint(x, y);
dis[i]=p[i].suan();
}
...
}
```
这样,您就可以通过 setPoint 函数来设置私有成员变量 x 和 y 的值。在 main 函数中,先声明 x 和 y 变量来存储输入的坐标值,然后使用 setPoint 函数来设置 Point 对象的坐标。
相关问题
/workspace/2.c/main.cpp:9:18: error: use of undeclared identifier 'sum'; did you mean 'sun'? printf ("%f",sum(first,second)) ; ^~~ sun /workspace/2.c/main.cpp:3:7: note: 'sun' declared here float sun(float,float); ^ 1 error generated.
这个错误提示说“sum”未定义,但是你定义的函数名字是“sun”,这是一个拼写错误。在第9行的 printf 语句中,你应该调用“sun”函数而不是“sum”函数。修改后的代码如下:
```
#include<iostream>
#include<stdio.h>
float sun(float, float);
int main() {
float first, second;
first = 123.23;
second = 99.09;
printf("%f", sun(first, second));
}
float sun(float a, float b) {
return a + b;
}
```
修改后的代码可以正确地计算并输出两个数的和。
/workspace/2.c/main.cpp:4:8: note: replace parentheses with an initializer to declare a variable int f( );
这个错误信息提示您需要使用初始化器来声明变量,而不是使用括号。在 C++ 中,使用括号来声明一个函数,但是使用括号来声明一个变量是非法的。
例如,如果您想声明一个整数变量,您应该这样写:
```
int x = 0;
```
而不是这样写:
```
int x();
```
如果您想声明一个没有参数的函数,您可以这样写:
```
int f() {
// 函数体
}
```
而不是这样写:
```
int f( );
```
希望能帮到你。