错误:不能将参数 2 从“const char”转换为“const char *求指教
#include<iostream>#include<string>usingnamespacestd;classString{protected:char*data;p...
#include<iostream>
#include<string>
using namespace std;
class String
{
protected:
char * data;
public:
String();
String(const char * pStr);
~String(){delete [] data;}
void Input();
};
String::String(const char *pStr)
{
if (pStr)
{
data = new char[strlen(pStr) + 1];
strcpy(data, pStr);
}
else
{
data = new char[1];
*data = '\0';
}
}
void String::Input()
{
int m;
char c1[50];
cout<<"姓名:";
cin>>c1;
m = strlen(c1);
delete [] data;
data = new char[m + 1];
strcpy(data,c1);
}
class Date
{
int year;
int month;
int day;
public:
Date()
{
year = month = day = 0;
}
void Input()
{
cout<<"出生日期:";
cin>>year>>month>>day;
}
};
class Employee:public String,public Date
{
protected:
string post;
char sex;
short department;
public:
Employee(string p,const char *pStr):String(*pStr)
{
post = p;
sex = 'm';
department = 0;
}
void Input()
{
String::Input();
Date::Input();
cout<<"性别(m为男,f为女):";
cin>>sex;
cout<<"部门:";
cin>>department;
}
};
class Manager:public Employee
{
short level;
public:
Manager(string p,const char *pStr):Employee(p,*pStr)
{
level = 0;
}
void Input1()
{
Employee::Input();
cout<<"级别:";
cin>>level;
}
void Output1()
{
cout<<"该员工的可得工资:"<<(10000 + level * 2000)<<endl;
}
};
class Technician:public Employee
{
short hours;
public:
Technician(string p,const char *pStr):Employee(p,*pStr)
{
hours = 0;
}
void Input2()
{
Employee::Input();
cout<<"工作小时:";
cin>>hours;
}
void Output2()
{
cout<<"该员工的可得工资:"<<160 * hours<<endl;
}
};
class Salesman:public Employee
{
float fsale;
public:
Salesman(string p,const char *pStr):Employee(p,*pStr)
{
fsale = 0;
}
void Input3()
{
Employee::Input();
cout<<"销售额:";
cin>>fsale;
}
void Output3()
{
cout<<"该员工的可得工资:"<<fsale * 0.05<<endl;
}
};
int main()
{
string post;
char c[] = "name";
cout<<"输入职位:";
cin>>post;
if(post == "manager")
{
Manager Manager1(post,c);
Manager1.Input1();
Manager1.Output1();
}
if(post == "technician")
{
Technician Technician1(post,c);
Technician1.Input2();
Technician1.Output2();
}
if(post == "salesman")
{
Salesman Salesman1(post,c);
Salesman1.Input3();
Salesman1.Output3();
}
return 0;
}
1>c:\users\cyd\documents\visual studio 2008\projects\第五次作业\6\6.cpp(62) : error C2664: “String::String(const char *)”: 不能将参数 1 从“const char”转换为“const char *”
1> 从整型转换为指针类型要求 reinterpret_cast、C 样式转换或函数样式转换
1>c:\users\cyd\documents\visual studio 2008\projects\第五次作业\6\6.cpp(82) : error C2664: “Employee::Employee(std::string,const char *)”: 不能将参数 2 从“const char”转换为“const char *”
1> 从整型转换为指针类型要求 reinterpret_cast、C 样式转换或函数样式转换 展开
#include<string>
using namespace std;
class String
{
protected:
char * data;
public:
String();
String(const char * pStr);
~String(){delete [] data;}
void Input();
};
String::String(const char *pStr)
{
if (pStr)
{
data = new char[strlen(pStr) + 1];
strcpy(data, pStr);
}
else
{
data = new char[1];
*data = '\0';
}
}
void String::Input()
{
int m;
char c1[50];
cout<<"姓名:";
cin>>c1;
m = strlen(c1);
delete [] data;
data = new char[m + 1];
strcpy(data,c1);
}
class Date
{
int year;
int month;
int day;
public:
Date()
{
year = month = day = 0;
}
void Input()
{
cout<<"出生日期:";
cin>>year>>month>>day;
}
};
class Employee:public String,public Date
{
protected:
string post;
char sex;
short department;
public:
Employee(string p,const char *pStr):String(*pStr)
{
post = p;
sex = 'm';
department = 0;
}
void Input()
{
String::Input();
Date::Input();
cout<<"性别(m为男,f为女):";
cin>>sex;
cout<<"部门:";
cin>>department;
}
};
class Manager:public Employee
{
short level;
public:
Manager(string p,const char *pStr):Employee(p,*pStr)
{
level = 0;
}
void Input1()
{
Employee::Input();
cout<<"级别:";
cin>>level;
}
void Output1()
{
cout<<"该员工的可得工资:"<<(10000 + level * 2000)<<endl;
}
};
class Technician:public Employee
{
short hours;
public:
Technician(string p,const char *pStr):Employee(p,*pStr)
{
hours = 0;
}
void Input2()
{
Employee::Input();
cout<<"工作小时:";
cin>>hours;
}
void Output2()
{
cout<<"该员工的可得工资:"<<160 * hours<<endl;
}
};
class Salesman:public Employee
{
float fsale;
public:
Salesman(string p,const char *pStr):Employee(p,*pStr)
{
fsale = 0;
}
void Input3()
{
Employee::Input();
cout<<"销售额:";
cin>>fsale;
}
void Output3()
{
cout<<"该员工的可得工资:"<<fsale * 0.05<<endl;
}
};
int main()
{
string post;
char c[] = "name";
cout<<"输入职位:";
cin>>post;
if(post == "manager")
{
Manager Manager1(post,c);
Manager1.Input1();
Manager1.Output1();
}
if(post == "technician")
{
Technician Technician1(post,c);
Technician1.Input2();
Technician1.Output2();
}
if(post == "salesman")
{
Salesman Salesman1(post,c);
Salesman1.Input3();
Salesman1.Output3();
}
return 0;
}
1>c:\users\cyd\documents\visual studio 2008\projects\第五次作业\6\6.cpp(62) : error C2664: “String::String(const char *)”: 不能将参数 1 从“const char”转换为“const char *”
1> 从整型转换为指针类型要求 reinterpret_cast、C 样式转换或函数样式转换
1>c:\users\cyd\documents\visual studio 2008\projects\第五次作业\6\6.cpp(82) : error C2664: “Employee::Employee(std::string,const char *)”: 不能将参数 2 从“const char”转换为“const char *”
1> 从整型转换为指针类型要求 reinterpret_cast、C 样式转换或函数样式转换 展开
1个回答
展开全部
参数应该是常量字符 不是常量字符指针 你的char *pStr的问题 应该是char pStr
追问
那能不能改main函数,让参数变成常量字符指针。请问怎么改?
追答
Employee(string p,const char *pStr):String(*pStr)
Manager(string p,const char *pStr):Employee(p,*pStr)
*pStr改成pStr就好 这里pStr是一个指针
只改后边继承的类里那个 const那个不用改
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询