在 c++ 中使用指向 char 的指针时,没有返回错误为 1 的输出
No output with a return error of 1 while using pointers to char in c++
我已经彻底寻找了一个无济于事的答案。我一直在使用从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("");
}
相关文章:
- (C++)分析树以计算返回错误值的简单算术表达式
- glad 导致 glfwSwapBuffers 返回错误消息
- 我的模板类方法返回错误类型?
- 共享内存:MapViewOfFile 返回错误 5
- 使用 std::forward_list 返回错误的队列实现
- FFMPEG:sws_scale返回错误:切片参数 0, 2160 无效
- __int64 CString 返回错误的值 - C++ MFC
- C++ OpenGL 中,绘制形状返回错误
- Python os.system() 返回错误值
- 函数从指针 c++ 中获取错误的值并返回错误的答案
- LibClang clang_getArgType() 返回错误的类型
- 除法函数返回错误的值
- 当命令失败时,gzip 会在 C++ 中返回错误代码吗?
- 矩阵逆变器返回错误的反转
- openCL-创建子缓冲区返回错误代码13
- Qt SQL LIKE语句返回错误
- GetLastError()从FtpPutFile()返回错误6
- PfCreateInterface返回错误120(未实现)
- 为什么 Pa_GetStreamTime() 返回错误?
- G++ 在尝试使用 G++ 使用 openCV 进行编译后返回错误