在 c++ 中使用指向 char 的指针时,没有返回错误为 1 的输出

No output with a return error of 1 while using pointers to char in c++

本文关键字:返回 错误 输出 指针 c++ char      更新时间:2023-10-16

我已经彻底寻找了一个无济于事的答案。我一直在使用从C++加迪斯教科书开始作为参考。我知道这个问题有一个简单的答案,但我很难弄清楚。这是我的代码:

class Employee
{
    private:
    char *name;
    int idNumber;
    char *department;
    char *position;
    void initName(const char *n)
{
    name = new char[strlen(n) + 1];
    strcpy(name, n);
}
void initDepartment(char *d)
{
    department = new char[strlen(d) + 1];
    strcpy(department, d);
}
void initPosition(char *p)
{
    position = new char[strlen(p) + 1];
    strcpy(position, p);
}
public:
Employee()
{
    strcpy(name, "");
    idNumber = 0;
    strcpy(department, "");
    strcpy(position, "");
}
Employee(char *nam, int num, char *dep, char *posit)
{
    initName(nam);
    idNumber = num;
    initDepartment(dep);
    initPosition(posit);
}
Employee(const char *nam, int num)
{
    initName(nam);
    idNumber = num;
    strcpy(department, "");
    strcpy(position, "");   
}
~Employee()
{
    delete [] name;
    delete [] department;
    delete [] position;
}
void setName(char *n)
{
    name = new char[strlen(n) + 1];
    strcpy(name, n);
}
void setIdNumber(int num)
{
    idNumber = num;
}
void setDepartment(char *d)
{
    department = new char[strlen(d) + 1];
    strcpy(department, d);
}
void setPosition(char *p)
{
    position = new char[strlen(p) + 1];
    strcpy(position, p);
}
const char * getName() const
{
    return name;
}
int getIdNumber() const
{
    return idNumber;
}
const char * getDepartment() const 
{
    return department;
}
const char * getPosition() const 
{
    return position;
}
};
int main(int argc, char** argv) 
{
const int SIZE = 50;
const char name[SIZE] = "Mark Jones";
Employee employee(name, 3452);
const char *ptr = NULL;
ptr = employee.getName();
cout << ptr << endl;
return 0;
}

在默认构造函数中,将空字符串复制到垃圾指针:

Employee()
// name and all other members are not initialized
{
    strcpy(name, ""); // << copy to unallocated memory
    idNumber = 0;
    strcpy(department, ""); // << and here
    strcpy(position, ""); // << and here
}

正确的默认构造函数是:

Employee()
: name(nullptr) // Initialize all members here
, idNumber(0)
, department(nullptr)
, position(nullptr)
{
    // Initialize c-strings
    initName("");
    initDepartment("");
    initPosition("");
}

不要忘记一直使用const。以及类似的其他构造函数:

Employee(const char* nam, int num, const char* dep, const char* posit)
: name(nullptr) // << Initialize all members here
, idNumber(num) // << Initialize number with proper value
, department(nullptr)
, position(nullptr)
{
    initName(nam);
    initDepartment(dep);
    initPosition(posit);
}
Employee(const char* nam, int num)
: name(nullptr) // Initialize all members here
, idNumber(num) // << Initialize number with proper value
, department(nullptr)
, position(nullptr)
{
    initName(nam);
    initDepartment("");
    initPosition("");   
}

正确的函数参数:

void initName(const char *n)
{
    name = new char[strlen(n) + 1];
    strcpy(name, n);
}
void initDepartment(const char *d)
{
    department = new char[strlen(d) + 1];
    strcpy(department, d);
}
void initPosition(const char *p)
{
    position = new char[strlen(p) + 1];
    strcpy(position, p);
}

并且不要忘记清除二传器中的内存(否则它是一个内存):

void setName(const char *n)
{
    delete[] name;
    name = new char[strlen(n) + 1];
    strcpy(name, n);
}
void setDepartment(const char *d)
{
    delete[] department;
    department = new char[strlen(d) + 1];
    strcpy(department, d);
}
void setPosition(char *p)
{
    delete[] position;
    position = new char[strlen(p) + 1];
    strcpy(position, p);
}

这应该可以修复所有运行时错误。

而是在构造函数中使用成员函数。构造函数启动时不会初始化指针,但成员函数将初始化它们:

Employee(const char *nam,int num)
{
    initName(nam);
    idNumber = num;
    //strcpy(department,"");
    //strcpy(position,"");
    initDepartment("");
    initPosition("");
}