gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:38:23 24 4
gpt4 key购买 nike

我已经彻底寻找答案无济于事。我一直在使用 Starting out with C++ Gaddis 教科书作为引用。我知道这个问题有一个简单的答案,但我很难弄明白。这是我的代码:

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);
}

并且不要忘记清除 setter 中的内存(否则就是内存泄漏):

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);
}

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

关于c++ - 在 C++ 中使用指向 char 的指针时没有返回错误 1 ​​的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37733944/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com