【发布时间】:2015-04-24 00:36:04
【问题描述】:
我正在将此头文件从使用 C++ 字符串类转换为使用 c 字符串(字符数组)。
我在语法上有困难。我知道将指针传递给 char 数组应该与传递字符串的方式相同。
我一直在努力编译几个小时,现在是时候寻求帮助了。编译器错误是内联的。任何帮助,将不胜感激。我要离开几个小时。我也可以发布 main 函数,但我认为问题是从这里开始的。
#ifndef EMPLOYEE_H_INCLUDED
#define EMPLOYEE_H_INCLUDED
#endif // EMPLOYEE_H_INCLUDED
using namespace std;
class Employee
{
private:
int id; // employee ID
char *name; // employee name
double hourlyPay; // pay per hour
int numDeps; // number of dependents
int type; // employee type
public:
//Employee(); // Default Constructor
Employee(int initId,const char *name,
double initHourlyPay ,
int initNumDeps , int initType); // Constructor
bool set(int newId, char newName[], double newHourlyPay,
int newNumDeps, int newType);
int getID(); // returns the employee ID
char getName(); // returns Employee name
int getDeps(); // returns number of dependents
float getRate(); // returns rate of pay
int getType(); // returns employee type
};
int Employee::getType(){
return type;
}
float Employee::getRate(){
return hourlyPay;
}
int Employee::getDeps(){
return numDeps;
}
int Employee::getID(){
return id;
}
到此为止。
|23|error: default argument missing for parameter 2 of 'Employee::Employee(int, char, double, int, int)'|
|62|error: prototype for 'Employee::Employee(int, char*, double, int, int)' does not match any in class 'Employee'|
|10|error: candidates are: Employee::Employee(const Employee&)|
char* Employee::getName(){
return name;
}
Employee::Employee( int initId, const char *name,
double initHourlyPay,
int initNumDeps, int initType )
{
bool status = set( initId, initName, initHourlyPay,
initNumDeps, initType );
if ( !status )
{
id = 0;
name = NULL;
hourlyPay = 0.0;
numDeps = 0;
type = 0;
}
}
bool Employee::set( int newId, char newName[20], double newHourlyPay,
int newNumDeps, int newType )
{
bool status = false;
if ( newId > 0 && newHourlyPay > 0 && newNumDeps >= 0 &&
newType >= 0 && newType <= 1 )
{
status = true;
id = newId;
name = newName;
hourlyPay = newHourlyPay;
numDeps = newNumDeps;
type = newType;
}
return status;
}
【问题讨论】:
-
你的包含保护没有做太多。目的是防止类的多个定义,但是不管
EMPLOYEE_H_INCLUDED是否定义,类定义都在那里。 -
我认为错误消息意味着您有一个地方可以使用
char而不是char*调用Employee构造函数。 -
哦'dat'粗心处理
char*...就像Barmar所说的,当你实际使用这个类时,可能在另一个实现中,你可能会用char来调用它char*参数 2. -
改变数据类型似乎很奇怪从
std::stringtochar*。 -
如果您需要将
const char *传递给某个接受字符指针的函数,为什么不直接使用std::string::c_str()?
标签: c++ c arrays string pointers