PS:我是初学者。
我尝试的完整代码如下:this_is_the_tree_I_Was_trying_to_code_up
错误出现在第30,50,63,74和82行,例如:
第30行:“int”之前的预期主表达式;“char”之前的预期主表达式;
第50行:“int”之前的预期主表达式;“char”之前的预期主表达式;
第63行:“int”之前的预期主表达式;“char”之前的预期主表达式;
第74行:“int”之前的预期主表达式;“char”之前的预期主表达式;“int”之前的预期主表达式;
第82行:“int”之前的预期主表达式;“char”之前的预期主表达式;“int”之前的预期主表达式;
#include <iostream>
#include <math.h>
#include <cstring>
using namespace std;
class staff{
protected:
int code;
char *name;
public:
staff(){}
staff(int code1,char *name1){
code=code1;
int length;
length=strlen(name1);
name=new char[length+1];
strcpy(name,name1);
}
void display(){
cout<<"Staff code is: "<<code<<"\n";
cout<<"Name is: "<<name<<"\n";
}
};
class teacher:public staff{
protected:
char *subject;
char *publication;
public:
teacher(){}
teacher(int code1,char *name1,char *sub1,char *pub1):
staff(int code1,char *name1){ //line 30
int len1,len2;
len1=strlen(sub1);
len2=strlen(pub1);
subject=new char[len1+1];
publication=new char[len2+1];
strcpy(subject,sub1);
strcpy(publication,pub1);
}
void display_t(){
cout<<"Subject is: "<<subject<<"\n";
cout<<"Publication is: "<<publication<<"\n";
}
};
class officer:public staff{
protected:
char grade;
public:
officer(){}
officer(int code1,char *name1,char grd1):
staff(int code1,char *name1){ //line 50
grade=grd1;
}
void display_o(){
cout<<"Grade is: "<<grade;
}
};
class typist:public staff{
protected:
int speed;
public:
typist(){}
typist(int code1,char *name1,int spd):
staff(int code1,char *name1){ //line 63
speed=spd;
}
void display_typ(){
cout<<"Speed is: "<<speed<<"\n";
}
};
class regular:public typist{
public:
regular(){}
regular(int code1,char *name1,int spd):
typist(int code1,char *name1,int spd){} //line 74
};
class casual:public typist{
protected:
float dailywage;
public:
casual(){}
casual(float wage,int code1,char *name1,int spd):
typist(int code1,char *name1,int spd){ //line 82
dailywage=wage;
}
void display_c(){
cout<<"Daily wage of casual typists are: "<<dailywage<<"\n";
}
};
int main()
{
casual c1;
c1=casual(2100.05,002,"Ganesh",50);
c1.display();
//c1.display_t();
c1.display_typ();
//c1.display_o();
c1.display_c();
return 0;
}
最佳答案
在这行上:
teacher(int code1,char *name1,char *sub1,char *pub1):
staff(int code1,char *name1) {
您使用了错误的语法来调用基类的构造函数。您只需要传递变量,而不传递类型。
这是正确编写它的方法:
teacher(int code1,char *name1,char *sub1,char *pub1):
staff(code1, name1) {
您所有其他错误都是由同一错误引起的,因此您需要对所有派生类的构造函数执行此操作。
关于c++ - 错误: 'int'之前的预期主表达式以及代码中的许多类似错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62392468/