下列程序段是否有错,若有错请改错完成程序。 #include<iostream.h> class point---------------------------------------------① { private: int x,y; public: void setpoint(int, int);--------------------② }; int point:: setpoint(int xx, int yy) ----------------③ { x=xx;--------------------------------------------- ④ y=yy;--------------------------------------------- ⑤ return 1;------------------------------------------ ⑥ } void main() { point p1;------------------------------------------------------------------------------ ⑦ p1.setpoint(2,4);--------------------------------------------------------------------- ⑧ cout<<”坐标为:(”<<p1.x<<”,”<<p1.y<<”)”<<endl;------------------------- ⑨ }
时间: 2023-08-15 18:19:04 浏览: 59
有错,以下是改错后的程序:
```cpp
#include<iostream>
using namespace std; //需要加上命名空间
class point {
private:
int x,y;
public:
void setpoint(int, int);
};
void point::setpoint(int xx, int yy) { //返回类型应为void
x=xx;
y=yy;
}
int main() { //main函数需要返回int类型
point p1;
p1.setpoint(2,4);
cout<<"坐标为:("<<p1.x<<","<<p1.y<<")"<<endl;
return 0;
}
```
改错点解释:
1. 第一行应该是 `#include<iostream>`,同时需要加上命名空间 `using namespace std;`。
2. 第一行后面的 `---------------------------------------------①` 应该是注释。
3. `setpoint()` 函数的返回类型应该是 `void`,因为它没有返回值。
4. `setpoint()` 函数的第一行应该是 `void point::setpoint(int xx, int yy)`,表示这是 `point` 类的成员函数。
5. `setpoint()` 函数中的变量名 `x` 和 `y` 应该与类中的同名变量对应。
6. `setpoint()` 函数的最后应该是 `return;`,因为它没有返回值。
7. `main()` 函数需要返回 `int` 类型。
8. `cout` 输出时,需要使用双引号 `"` 而不是中文引号 `”`。
9. `cout` 输出时,需要使用 `p1` 对象的成员变量 `x` 和 `y`。