【问题标题】:c strings vs string class syntax with class member functionsc字符串与带有类成员函数的字符串类语法
【发布时间】: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::string to char*
  • 如果您需要将const char * 传递给某个接受字符指针的函数,为什么不直接使用std::string::c_str()

标签: c++ c arrays string pointers


【解决方案1】:

这里有一个问题:

Employee::Employee( int initId, const char *name, // <-- name
                    double initHourlyPay,
                    int initNumDeps, int initType )
{
    bool status = set( initId, initName, initHourlyPay, // <-- initName
                       initNumDeps, initType );

我根据代码添加了几个 cmets; 注释 &lt;-- 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[] looksfeels 就像你的意思只是让函数临时访问char的数组 (要么从中复制一些东西,要么给它写一些东西,或者两者兼而有之), 但实际上它通过存储使该数组成为对象的一部分 它在成员变量中的地址。

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

【讨论】:

  • 非常感谢。我一直在尝试不同的东西,我的代码变得越来越糟。我遵循了在任何地方都使用 char * 的建议,并且我摆脱了 initName 与 newName 的不一致。头文件正在编译,我回到调试主要功能。我会掌握的!
猜你喜欢
  • 2021-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-16
  • 2019-03-22
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多