|
两个问题:
第一个问题:const char*与char*的区别(结合下面给出的例子),我清楚const表示的就是静态的,但是在下面给出例子中,student.cpp文件的Student::Student(int id, const char* pname, char sex, int age) 不能写为Student::Student(int id, char* pname, char sex, int age) ,给出的错误提示是因为test.cpp的Student stu2(1,"zhangsan",'m',21)这种表达不能与该构造函数相匹配
第二个问题::那个把属性name改为string类型,而不是name的char类型的数组,
当然构造函数也改变Student::Student(int id, const char* pname, char sex, int age),形参改为string类型,为什么不能使用用strcpy函数,错误提示原因是string类型与const char *类型不能转换过来
//student.cpp
#include "Student.h"
#include <cstring>
#include <string>
Student::Student() {
id = 0;
memset(name, 0, 50);
age = 10;
sex = 'm';
}
Student::Student(int id, const char* pname, char sex, int age) {
this->id = id;
strcpy(name,pname);
//memset(name, 0, 50);
this->age = age;
this->sex = sex;
}
//student.h
#define _CRT_SECURE_NO_DEPRECATE
#pragma once
//#pragma warning(suppress : 4996)
//#pragma warning(disable : 4996)
class Student
{
public:
int id;
char name[50];
char sex;
int age;
Student();
Student(int id,const char* pname,char sex,int age);
};
//test.cpp
#include <iostream>
#include "Student.h"
using namespace std;
int main() {
Student stu1;
Student stu2(1,"zhangsan",'m',21);
return 0;
}
|
上一篇: ADO打开SQL SEVER 2012 出错下一篇: QT问题:界面在运行,但不显示
|