|
我按照课上的内容在自己的vs2017上编译时总是出现下面这个错误:
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C2664 “CStudent::CStudent(const CStudent &)”: 无法将参数 1 从“const char [9]”转换为“char *”
以下是代码,就是照着第35课敲的。
#include<iostream>
#include<string>
using namespace std;
class CStudent
{
public:
char name[50];
char sex;
int num;
int age;
CStudent(char * pname, char t_sex, int t_num, int t_age);
~CStudent();
};
CStudent::CStudent(char * pname, char t_sex, int t_num, int t_age):sex(t_sex),num(t_num),age(t_age)
{
strcpy_s(name, pname);
cout << "CStudent called " << endl;
//数组都名字代表首地址,所以输入为指针。
}
CStudent::~CStudent()
{
cout << " ~CStudnet called " << endl;
}
void test()
{
CStudent stu("zhangsan",'f',1001,19);
}
int main()
{
//CStudent stu("zhangsan", 'f', 1001, 20);
//stu = ( "zhangshan", 'f', 10001, 16 );
test();
return 0;
}
本帖最后由 cpp2019 于 2021-8-6 12:55 编辑
- // 一、强转
- CStudent stu((char*)"zhangsan", 'f', 1001, 19);
- // 二、传数组
- char szName[] = "zhangsan";
- CStudent stu(szName, 'f', 1001, 19);
- // 三、还可以修改构造器的参数
- // 四、把符合模式关了,兼容老项目,不用改代码
复制代码
|
上一篇: 22课作业下一篇: WebBrowser滑块指定位置没效果问题
|