0
点赞
收藏
分享

微信扫一扫

char*引发的一些错误


首先了解一下char* 和 char[]区别,

这篇文章【C/C++】对char* 和 char[]区别的一些理解

写的很好,推荐看看。

在VS2019中,原来错误的代码

#include <iostream>

class Student
{
private:
int m_number;
char* m_name;
public:
Student(int number,const char* name);//构造函数
Student(const Student &s);//拷贝构造函数
void show();
void setNumber(int number);
void setName(char* name);
};

Student::Student(int number,char* name)
{
m_number = number;
m_name = new char[strlen(name) + 1]; //申请内存
strcpy(m_name, name);
}
Student::Student(const Student &s)
{
m_number = s.m_number;
m_name = new char[strlen(s.m_name) + 1]; //申请内存
strcpy(m_name, s.m_name);
}

void Student::setNumber(int number)
{
m_number = number;
}

void Student::setName(char* name)
{
if (m_name != nullptr)
{
delete []m_name;
m_name = new char[strlen(name) + 1];
strcpy(m_name, name);

}
}

void Student::show()
{
std::cout << "学号:" << m_number << "\t" << "姓名:" << m_name << std::endl;
}

int main()
{
Student s1(20210001,"张三");
s1.setName("张小三");
s1.show();
return 0;
}

报错1:

E0289 没有与参数列表匹配的构造函数 “Student::Student” 实例

char*引发的一些错误_#include

错误分析

char*引发的一些错误_c++_02

张三这个字符串为const char [5] 类型,而类中的构造函数不同。

解决方法

将 char * name 改写为const char* name

报错2:

error C4996: ‘strcpy’: This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.char*引发的一些错误_c++_03错误分析​:使用VS2005以上版本(VS2008、VS2010、VS2013、VS2015、VS2017、VS2019)编译在其他编译器下正常通过的C语言程序都有可能遇到。

这种微软的警告,主要因为那些C库的函数,很多函数内部是不进行参数检测的(包括越界类的),微软担心使用这些会造成内存异常,所以就改写了同样功能的函数,改写了的函数进行了参数的检测,使用这些新的函数会更安全和便捷。库函数改写例子:

mkdir改写为 _mkdir

fopen改写为 fopen_s

stricmp改写为 stricmp_s

strcpy改写为strcpy_s

scanf改写为scanf_s

解决方法1

找到【项目属性】,点击【C/C++】里的【预处理器】,对【预处理器】进行编辑,在里面加入一段代码:_CRT_SECURE_NO_WARNINGS。

char*引发的一些错误_javascript_04

修改后:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

class Student
{
private:
int m_number;
char* m_name;
public:
Student(int number,const char* name);
Student(const Student &s);
void show();
void setNumber(int number);
void setName(const char* name);
};

Student::Student(int number,const char* name)
{
m_number = number;
m_name = new char[strlen(name) + 1]; //申请内存
strcpy(m_name, name);
}

Student::Student(const Student &s)
{
m_number = s.m_number;
m_name = new char[strlen(s.m_name) + 1]; //申请内存
strcpy(m_name, s.m_name);
}

void Student::setNumber(int number)
{
m_number = number;
}

void Student::setName(const char* name)
{
if (m_name != nullptr)
{
delete []m_name;
m_name = new char[strlen(name) + 1];
strcpy(m_name, name);

}
}

void Student::show()
{
std::cout << "学号:" << m_number << "\t" << "姓名:" << m_name << std::endl;
}

int main()
{
Student s1(20210001,"张三");
s1.setName("张小三");
s1.show();
return 0;
}

解决方法2:

根据报错提示,用strcpy_s替代strcpy。

#include <iostream>
#include <string>

class Student
{
private:
int m_number;
char* m_name;
public:
Student(int number,const char* name);
Student(const Student &s);
void show();
void setNumber(int number);
void setName(const char* name);
};

Student::Student(int number,const char* name)
{
m_number = number;
m_name = new char[strlen(name) + 1]; //申请内存
strcpy_s(m_name, strlen(name) + 1, name);
}

Student::Student(const Student &s)
{
m_number = s.m_number;
m_name = new char[strlen(s.m_name) + 1]; //申请内存
strcpy_s(m_name, strlen(s.m_name) + 1, s.m_name);
}

void Student::setNumber(int number)
{
m_number = number;
}

void Student::setName(const char* name)
{
if (m_name != nullptr)
{
delete []m_name;
m_name = new char[strlen(name) + 1];
strcpy_s(m_name, strlen(name) + 1, name);

}
}

void Student::show()
{
std::cout << "学号:" << m_number << "\t" << "姓名:" << m_name << std::endl;
}

int main()
{
Student s1(20210001,"张三");
s1.setName("张小三");
s1.show();
return 0;
}

结果:

学号:20210001  姓名:张小三



举报

相关推荐

0 条评论