#include<iostream>
#include<string.h>
using namespace std;
class Person{
protected:
char m_strName[10];
int m_nAge;
int m_nSex;
public:
void Register(char* name, int age, char sex){
strcpy(m_strName, name);
m_nAge = age;
m_nSex = sex;
}
char* GetName() { return m_strName; }
int GetAge() { return m_nAge; }
char GetSex() { return m_nSex == 0 ? 'm' : 'f'; }
void ShowMe() {
cout << GetName() << "\t" << GetSex() << "\t" << GetAge() << "\t" << endl;
}
};
class Employee:public Person {
char m_strDept[20];
float m_fSalary;
public:
Employee(){ EmployeeRegister("XXX", 0,'m', "XXX", 0); }
void EmployeeRegister(char* name, int age, char sex, char* dept, float salary);
void ShowMe();
};
void Employee::EmployeeRegister(char* name, int age, char sex, char* dept, float salary) {
Register(name, age, sex);
strcpy(m_strDept, dept);
m_fSalary = salary;
}
void Employee::ShowMe() {
cout << GetName() << "\t" << GetSex() << "\t" << GetAge() << "\t";
cout << m_strDept << "\t" << m_fSalary << endl;
cout << m_strName << endl;
}
void main() {
Employee emp;
emp.ShowMe();
emp.EmployeeRegister("张莉", 40, 'f', "图书馆", 2000);
emp.ShowMe();
emp.Person::ShowMe();
cout << "调用基类 GetName() 返回值为:" << emp.GetName() << endl;
}
能不能帮我看一下到底错哪里了,老是显示strcpy啥的
有几个问题需要解决:
修复后的代码如下所示:
#include <iostream>
#include <cstring>
using namespace std;
class Person {
protected:
char m_strName[10];
int m_nAge;
char m_nSex;
public:
void Register(char* name, int age, char sex) {
strcpy(m_strName, name);
m_nAge = age;
m_nSex = sex;
}
char* GetName() { return m_strName; }
int GetAge() { return m_nAge; }
char GetSex() { return m_nSex == 0 ? 'm' : 'f'; }
void ShowMe() {
cout << GetName() << "\t" << GetSex() << "\t" << GetAge() << "\t" << endl;
}
};
class Employee : public Person {
char m_strDept[20];
float m_fSalary;
public:
Employee() { EmployeeRegister("XXX", 0, 'm', "XXX", 0); }
void EmployeeRegister(char* name, int age, char sex, char* dept, float salary);
void ShowMe();
};
void Employee::EmployeeRegister(char* name, int age, char sex, char* dept, float salary) {
Register(name, age, sex);
strcpy(m_strDept, dept);
m_fSalary = salary;
}
void Employee::ShowMe() {
cout << GetName() << "\t" << GetSex() << "\t" << GetAge() << "\t";
cout << m_strDept << "\t" << m_fSalary << endl;
cout << m_strName << endl;
}
int main() {
Employee emp;
emp.ShowMe();
emp.EmployeeRegister("张莉", 40, 'f', "图书馆", 2000);
emp.ShowMe();
emp.Person::ShowMe();
cout << "调用基类 GetName() 返回值为: " << emp.GetName() << endl;
return 0;
}
运行结果:
函数不安全,新版本的vs对这种不安全的函数都会报错的,你可以去搜索用安全的函数代替
开头加上
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string.h>
using namespace std;
class Person{
protected:
char m_strName[10];
int m_nAge;
char m_nSex;
public:
void Register(char* name, int age, char sex){
strcpy(m_strName, name);
m_nAge = age;
m_nSex = sex;
}
char* GetName() { return m_strName; }
int GetAge() { return m_nAge; }
char GetSex() { return m_nSex == 0 ? 'm' : 'f'; }
void ShowMe() {
cout << GetName() << "\t" << GetSex() << "\t" << GetAge() << "\t" << endl;
}
};
class Employee:public Person {
char m_strDept[20];
float m_fSalary;
public:
Employee(){ EmployeeRegister("XXX", 0,'m', "XXX", 0); }
void EmployeeRegister(char* name, int age, char sex, char* dept, float salary);
void ShowMe();
};
void Employee::EmployeeRegister(char* name, int age, char sex, char* dept, float salary) {
Register(name, age, sex);
strcpy(m_strDept, dept);
m_fSalary = salary;
}
void Employee::ShowMe() {
cout << GetName() << "\t" << GetSex() << "\t" << GetAge() << "\t";
cout << m_strDept << "\t" << m_fSalary << endl;
cout << m_strName << endl;
}
int main() {
Employee emp;
emp.ShowMe();
emp.EmployeeRegister("张莉", 40, 'f', "图书馆", 2000);
emp.ShowMe();
emp.Person::ShowMe();
cout << "调用基类 GetName() 返回值为:" << emp.GetName() << endl;
}
再比如我们调用C函数strcpy将一个字符串拷贝到目标buffer:
char szDstBuf[10] = { 0 };
char* pInfo = "this is a test";
strcpy(szDstBuf, pInfo);
显然这个"this is a test"字符串长度为14字节(加上\0结尾符则是15),已经超过了目标buffer szDstBuf的长度(10字节),所以执行strcpy后就发生内存越界了。你如果要将这段代码拷贝到visual studio中运行,这段代码执行后会报如下的错误:
即szDstBuf局部变量周边的栈内存被破坏了,为啥这么说呢?因为执行strcpy时内存越界了,篡改了szDstBuf变量后面的内存了。
在给出具体解决方案之前,需要获取更多关于代码错误的信息,因此需要询问以下问题以获取更多信息:
您正在使用哪种编程语言?
您使用的是哪个版本的strcpy函数?
您是否可以提供代码片段以供参考?
根据参考资料中的信息,可以给出以下可能有用的解决方案:
确保目标字符串长度足够大,以避免出现缓冲区溢出错误。
检查源字符串和目标字符串指针是否为空。
确保您的代码中没有其他语法错误或逻辑错误,这些错误可能导致strcpy函数无法正常工作。
确保您的代码中使用的是正确版本的strcpy函数,不同版本的函数可能在实现方式上存在差异,导致错误。
给出具体解决方案需要进一步了解关于代码错误的详细信息。