Main.cpp:34:142: error: expected primary-expression before 'f' Stu(string f = " ", string a = "", string b = " ", string c = " ", string d = " ", int aa=0, int bb=0, int cc=0, int dd=0):Person(string f = " ", string a = "", string b = " ", string c = " ", string d = " ")
时间: 2023-08-21 22:06:22 浏览: 239
expected primary-expression before token (解决方案).md
这个错误是因为你在类的构造函数声明中给参数设置了默认值,但是在调用该构造函数时没有传递任何参数。如果你想使用默认值,可以不传递这些参数,否则你需要传递与参数数量和类型匹配的实参。
以下是一个例子:
```c++
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
string name;
int age;
Person(string n = "", int a = 0) : name(n), age(a) {}
};
class Student : public Person {
public:
string school;
int grade;
Student(string n = "", int a = 0, string s = "", int g = 0) : Person(n, a), school(s), grade(g) {}
};
int main() {
Person p("Tom", 20);
Student s("Jerry", 18, "ABC School", 12);
cout << p.name << " " << p.age << endl;
cout << s.name << " " << s.age << " " << s.school << " " << s.grade << endl;
return 0;
}
```
在上面的代码中,Person类和Student类都有构造函数,并且都有默认参数。在Student类的构造函数中,我们通过调用父类Person的构造函数来初始化name和age成员变量。在main函数中,我们分别创建了一个Person对象和一个Student对象,并输出了它们的成员变量。
阅读全文