c字符串与具有类成员函数的字符串类语法
c strings vs string class syntax with class member functions
我正在将这个头文件从使用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
显示了试图传递名为initName
到set
函数,但没有用声明的变量该名称;相反,四行之前有一个名为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_。
- 字符串函数在目标C++上运行时C++返回空
- 字符串函数返回奇怪的值
- 这里的字符串函数是什么意思
- 在字符串函数中抛出'char const*'实例后调用的终止
- 为什么 stl 字符串C++函数有时出错有时会出错?
- 从C++为字符串函数设置_ENV
- 来自 2D 数组的字符串函数
- 返回最长的公共子字符串函数 c ++
- 使用getline和字符串函数C++程序中的分段错误
- 在字符串函数的指针中返回地址
- 字符串函数:strcat()
- 这个子字符串函数有什么问题
- 字符串函数不返回字符串? C++
- 使用向量和字符串函数返回字符串
- 尝试修复一个字符串函数,该函数采用字符串并通过替换一些单词来更改它
- 逐字读取字符数组,无需字符串函数
- 我从我的字符串函数返回到主函数
- 为什么我不能从字符串函数返回字符串数组?
- 如何在不使用字符串函数的情况下在char数组中找到字符串?(C )
- 带有 #include< iostream.h> 的程序允许使用 strlen() 等字符串函数。为什么?