#include <iostream> using namespace std; class student { private: string name; public: int classnum, num; int age; int value(){return age;} student() { age = 0; num = 132; name = "lily"; } student(int n) { age = n; num = n; name = "brian"; } }; string student::getname() { return name; } int main() { student A(32), B; cout << A.value() << endl; cout << A.num << endl; cout << A.getname << endl; cout << B.age << " " << B.num << " " << B.getname() << " " << B.value(); return 0; }这个代码语法错在哪
时间: 2024-01-14 19:02:45 浏览: 57
使用include <iostream>时一定要加using namespace std
这个代码有两个语法错误:
1. 在 `student` 类中,没有声明 `getname()` 函数,但是在 `main()` 函数中却调用了这个函数。
2. 在 `main()` 函数中,调用 `getname()` 函数时没有加上 `()`。
修正后的代码如下:
```
#include <iostream>
using namespace std;
class student {
private:
string name;
public:
int classnum, num;
int age;
int value() { return age; }
student() {
age = 0;
num = 132;
name = "lily";
}
student(int n) {
age = n;
num = n;
name = "brian";
}
string getname() { return name; }
};
int main() {
student A(32), B;
cout << A.value() << endl;
cout << A.num << endl;
cout << A.getname() << endl;
cout << B.age << " " << B.num << " " << B.getname() << " " << B.value();
return 0;
}
```
阅读全文