指出下列程序中的错误,并说明为什么。(抄题,在错误语句右侧标注错误原因,并改正)# include< iostream>using namespace std;class Student{int sno;int age;void printStu ();void setSno (int d);};void printStu (){ cout<< "\nSno is"<<sno<<","Cout<<"age is"<< age<< ". "<<endl;}void setSno (int s){ sno=s;}void setAge (int a){ age=a;}int main (){ Student lin;lin.setSno (20021);lin.setAge (20);lin.printStu ();}
时间: 2024-03-23 22:37:06 浏览: 21
错误:程序中存在多个错误。
1. 在类的定义中,函数没有访问修饰符。
2. 在类的定义中,函数的声明和实现不能分开。
3. 在类的定义中,缺少了一个函数的声明。
4. 在类的定义中,声明了一个函数,但没有实现。
5. 在 printStu 函数中,字符串常量的引号不匹配。
6. 在 printStu 函数中,变量名字母大小写错误。
7. 在 main 函数中,缺少了一个头文件的引用。
改正后的程序如下:
```c++
#include <iostream>
using namespace std;
class Student {
private:
int sno;
int age;
public:
void printStu();
void setSno(int d);
void setAge(int a);
};
void Student::printStu() {
cout << "\nSno is " << sno << ", age is " << age << ". " << endl;
}
void Student::setSno(int s) {
sno = s;
}
void Student::setAge(int a) {
age = a;
}
int main() {
Student lin;
lin.setSno(20021);
lin.setAge(20);
lin.printStu();
return 0;
}
```
相关问题
1、指出下列程序中的错误,并说明为什么。(抄题,在错误语句右侧标注错误原因,并改正) # include< iostream> using namespace std; class Student{ int sno; int age; void printStu (); void setSno (int d); }; void printStu () { cout<< "\nSno is"<<sno<<"," Cout<<"age is"<< age<< ". "<<endl; } void setSno (int s) { sno=s; } void setAge (int a) { age=a; } int main () { Student lin; lin.setSno (20021); lin.setAge (20); lin.printStu (); },并将改正的代码打出来
错误:1. 在类定义中,分号后面不应该有多余的空格。2. 类中的方法定义需要声明在类的内部,而不是在类的外部。3. printStu() 和 setSno() 方法的定义应该在类的外部添加 Student:: 前缀来指定所属的类。4. 在 printStu() 方法中,第二个 cout 应该小写。5. 在 setAge() 方法中,函数名应该是 setAge() 而不是 setage()。
改正后的代码如下:
```
#include <iostream>
using namespace std;
class Student {
private:
int sno;
int age;
public:
void printStu();
void setSno(int d);
void setAge(int a);
};
void Student::printStu() {
cout << "\nSno is " << sno << ", age is " << age << ". " << endl;
}
void Student::setSno(int s) {
sno = s;
}
void Student::setAge(int a) {
age = a;
}
int main() {
Student lin;
lin.setSno(20021);
lin.setAge(20);
lin.printStu();
return 0;
}
```
#include<easyx.h>错误
#include<easyx.h>是一个常见的错误,因为easyx是一个非标准的图形库,它不是C++标准库的一部分。所以在使用easyx库时,需要进行一些配置和设置。
首先,你需要确保你已经正确安装了easyx库,并且将其添加到你的编译器的包含路径中。这可以通过在编译器的设置中添加easyx库的路径来完成。
其次,你需要在你的代码中正确引用easyx库。通常情况下,你需要在代码的开头使用以下语句来引用easyx库:
```cpp
#include <graphics.h>
```
如果你仍然遇到错误,可能是由于以下原因之一:
1. 编译器无法找到easyx库的路径。请确保你已经正确设置了编译器的包含路径,并且路径中包含了easyx库的位置。
2. 缺少必要的依赖项。easyx库可能依赖于其他一些库或者组件,你需要确保这些依赖项已经正确安装并且可用。
3. 编译器不支持easyx库。某些编译器可能不支持非标准的库,或者需要进行额外的配置才能使用。你可以尝试使用其他编译器或者查阅编译器的文档以获取更多信息。
希望以上回答对你有所帮助!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)