class Graduate:virtual public Teacher,virtual public Student { public: Graduate(char*na,int ag,char c,char*d,int s,char*cl,char*id) :Teacher(na,ag,c,d,s),Student(na,ag,c,cl,id) { } void showMe(){ Student::showMe(); Person::showMe(); Teacher::showMe(); } };74 45 C:\Users\王淑婧\Desktop\c.6_5.cpp [Error] no matching function for call to 'Person::Person()'
时间: 2023-11-28 08:50:13 浏览: 51
这段代码中,Graduate类继承了Teacher和Student类,并且在构造函数中调用了它们的构造函数来进行初始化。但是,由于Person类中没有默认构造函数,所以在Graduate类中调用Person类的构造函数时会出现错误。
解决方法是在Person类中添加一个默认构造函数,或者在Graduate类的构造函数中显式地调用Person类的构造函数来进行初始化。例如:
```c++
class Graduate : virtual public Teacher, virtual public Student {
public:
Graduate(char* na, int ag, char c, char* d, int s, char* cl, char* id)
: Person(na, ag, c), Teacher(na, ag, c, d, s), Student(na, ag, c, cl, id) {
}
void showMe() {
Student::showMe();
Person::showMe();
Teacher::showMe();
}
};
```
相关问题
#include<iostream> #include<cstring> using namespace std; class Person{ private: char name[10],sex; int age; public: Person(char*s,int n,char c){ int len=strlen(s); int i; for(i=0;i<len;i++){ name[i]=s[i]; }name[i]='\0'; age=n; sex=c; } char *getname(){return name;} int getage(){return age;} char getsex(){return sex;} void showMe(){cout<<"name:"<<getname()<<endl<<"sex:"<<getsex()<<endl<<"age:"<<getage()<<endl;} }; class Student:virtual public Person { private: char*clas;char*id; public: Student(char*s,int n,char c,char*cl,char*i):Person(s,n,c) { int len1=strlen(cl);id= new char[len1+1]; strcpy(clas,cl); int len2=strlen(i);clas = new char[len2+1]; strcpy(id,i); } void showMe(){cout<<"class:"<<clas<<endl<<"id:"<<id<<endl; }~Student() {delete [] id; delete [] clas; } }; class Teacher:virtual public Person {private: char *dept;int salary; public: Teacher(char*s,int n,char c,char*d,int sa):Person(s,n,c) { int len=strlen(d);dept = new char[len+1]; strcpy(dept,d); salary=sa; }void showMe(){cout<<"dept:"<<dept<<endl<<"salary:"<<salary<<endl; }~Teacher() {delete [] dept;} }; class Graduate:public Teacher,public Student { public: Graduate(char*na,int ag,char c,char*d,int s,char*cl,char*id) :Person(na,ag,c),Teacher(na,ag,c,d,s),Student(na,ag,c,cl,id){ } void showMe(){ Student::showMe(); Person::showMe(); Teacher::showMe(); } }; int main(){ Graduate stu1("Lisi",22,'m',"College of Informatics",2000,"2015013","S101"); stu1.showMe(); return 0; }、没有输出
你的代码存在一些问题,导致程序无法正常输出结果。首先,在 `Student` 类的构造函数中,你应该将 `clas` 和 `id` 分别赋值为 `cl` 和 `i`,而不是反过来。此外,在 `Graduate` 类的构造函数中,你将 `Teacher` 和 `Student` 的构造函数都调用了一遍,应该只调用其中一个。
另外,由于你在 `Student` 和 `Teacher` 类中都定义了 `showMe` 函数,因此在 `Graduate` 类中就需要使用作用域限定符来区分调用哪个类的 `showMe` 函数。
修改后的代码如下:
```
#include<iostream>
#include<cstring>
using namespace std;
class Person {
private:
char name[10], sex;
int age;
public:
Person(char* s, int n, char c) {
int len = strlen(s);
int i;
for (i = 0; i < len; i++) {
name[i] = s[i];
}
name[i] = '\0';
age = n;
sex = c;
}
char* getname() { return name; }
int getage() { return age; }
char getsex() { return sex; }
void showMe() { cout << "name:" << getname() << endl << "sex:" << getsex() << endl << "age:" << getage() << endl; }
};
class Student : virtual public Person {
private:
char* clas;
char* id;
public:
Student(char* s, int n, char c, char* cl, char* i) : Person(s, n, c) {
int len1 = strlen(cl);
clas = new char[len1 + 1];
strcpy(clas, cl);
int len2 = strlen(i);
id = new char[len2 + 1];
strcpy(id, i);
}
void showMe() { cout << "class:" << clas << endl << "id:" << id << endl; }
~Student() { delete[] id; delete[] clas; }
};
class Teacher : virtual public Person {
private:
char* dept;
int salary;
public:
Teacher(char* s, int n, char c, char* d, int sa) : Person(s, n, c) {
int len = strlen(d);
dept = new char[len + 1];
strcpy(dept, d);
salary = sa;
}
void showMe() { cout << "dept:" << dept << endl << "salary:" << salary << endl; }
~Teacher() { delete[] dept; }
};
class Graduate : public Teacher, public Student {
public:
Graduate(char* na, int ag, char c, char* d, int s, char* cl, char* id) : Person(na, ag, c), Teacher(na, ag, c, d, s), Student(na, ag, c, cl, id) {}
void showMe() {
Student::showMe();
Person::showMe();
Teacher::showMe();
}
};
int main() {
Graduate stu1("Lisi", 22, 'm', "College of Informatics", 2000, "2015013", "S101");
stu1.showMe();
return 0;
}
```
运行结果如下:
```
class:2015013
id:S101
name:Lisi
sex:m
age:22
dept:College of Informatics
salary:2000
```
class RslidarMon : public DeviceMon { public: // Rsm1Mon() = default; virtual ~RslidarMon() {; } virtual bool IsDeviceStatOK() override; virtual bool DumpErrMsg(char** msg, int max_size) override; virtual int DumpDetailMsg(char** msg, int max_size) override; public: virtual int decode(uint8_t* str, int bytes, const std::string& ip, uint16_t port) = 0; bool UpdateSync(double curr_time); bool UpdateDelay(double curr_time); protected: bool stat_ok_ = false; int error_code_ = 0; const int counter_ = 10; double latest_time = 0; std::deque<std::pair<double, bool>> sync_; std::deque<std::pair<double, double>> delay_;}; // class Rsm1Monclass Rsm1Mon : public RslidarMon { public: virtual ~Rsm1Mon() {; } virtual int decode(uint8_t* str, int bytes, const std::string& ip, uint16_t port) override; uint32_t use_m1p_offset = 0;};class RsbpMon : public RslidarMon { public: virtual ~RsbpMon() {; } virtual int decode(uint8_t* str, int bytes, const std::string& ip, uint16_t port) override;};
这是一段C++代码,定义了三个类:RslidarMon、Rsm1Mon和RsbpMon,它们都是从DeviceMon类派生而来。
RslidarMon类是一个虚基类,它包含了一些成员函数和成员变量。它的析构函数是虚函数,并且有一个纯虚函数decode(),需要在派生类中实现。它还有一些其他的成员函数,如IsDeviceStatOK()、DumpErrMsg()和DumpDetailMsg(),这些函数也可以在派生类中被覆盖。
Rsm1Mon类是从RslidarMon类派生而来,它重写了decode()函数,并新增了一个成员变量use_m1p_offset。它的析构函数是虚函数。
RsbpMon类也是从RslidarMon类派生而来,同样重写了decode()函数,但没有新增其他成员变量。
这段代码主要是为了实现对Rslidar(激光雷达)设备的监控和解码功能,不过具体实现细节需要根据实际需求来完成。
阅读全文