c字符串与具有类成员函数的字符串类语法

c strings vs string class syntax with class member functions

本文关键字:字符串 函数 语法 成员      更新时间:2023-10-16

我正在将这个头文件从使用C++字符串类转换为使用C字符串(字符数组)。

我在语法上有困难。我知道传递指向char数组的指针应该与传递字符串的方式相同。

几个小时以来,我一直在努力编译它,现在是时候寻求一些帮助了。编译器错误是内联的。如有任何帮助,我们将不胜感激。我要离开几个小时。我也可以发布主要功能,但我认为问题从这里开始。

#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::Employee( int initId, const char *name, // <-- name
double initHourlyPay,
int initNumDeps, int initType )
{
bool status = set( initId, initName, initHourlyPay, // <-- initName
initNumDeps, initType );

我添加了一些与代码一致的注释;注释<-- initName显示了试图传递名为initNameset函数,但没有用声明的变量该名称;相反,四行之前有一个名为name的变量。

我认为这也是不推荐的:

bool Employee::set( int newId, char newName[20], double newHourlyPay,
int newNumDeps, int newType )

为什么要指定字符串的确切长度?它不会在其他地方发生。给出了与char newName[]相同的函数参数在函数声明中。事实上,在set函数中,您有以下内容:

name = newName;

所以我宁愿看到参数在两个位置都声明为char* newName(函数声明和函数定义)。它没有任何意义与编译器不同,但传递char newName[]看起来,而感觉就像你的意思一样,只是为了让函数临时访问char的数组(要么从中复制一些东西,要么写一些东西给它,或者两者兼而有之),但实际上,它是通过存储它在成员变量中的地址。

这让人想起了这个程序中的另一个异常,那就是将成员变量name存储为char*,但地址作为CCD_ 12被传递到构造函数中。这可能会导致额外的编译问题。我建议在传递此字符串的所有位置使用char*如果您想复制指针并能够稍后修改字符串的内容,或者使用const char*作为构造函数中的函数参数和在set函数中,并将其存储在const char*中或者制作字符串的新副本(可以将其存储为CCD_ 17或作为CCD_。