|
- #include <iostream>
- #include <Cstring>
- using namespace std;
- class Cstudent
- {
- private:
- char* name;
- char sex;
- int num;
- int age;
- public:
- Cstudent(char* p_name, char p_sex, int p_num, int p_age): sex(p_sex), num(p_num), age(p_age)
- {
- int longx = strlen(p_name)+1;
- name = new char (longx);
- memset(name, 0, longx);
- strcpy(name, p_name);
- }
- ~Cstudent()
- {
- if(name)
- {
- delete name;
- name = NULL;
- }
- }
- bool operator==(const Cstudent* stud);
- Cstudent& operator=(const Cstudent* stud);
- void print_f()
- {
- cout << name << "--" << sex << "--" << num << "--" << age << endl;
- }
- };
- bool Cstudent:: operator==(const Cstudent* stud)
- {
- int count = strcmp(this->name, stud->name);
- if(count == 0 && (this->sex == stud->sex) &&
- (this->num == stud->num) && (this->age == stud->age))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
- Cstudent& Cstudent::operator=(const Cstudent* stud)
- {
- int n_len = 0;
- n_len = strlen(stud->name);
- name = new char[n_len + 1];
- memset(name, 0, n_len + 1);
- strcpy(name, stud->name);
- num = stud->num;
- age = stud->age;
- sex = stud->sex;
- return *this;
- }
-
- int main ()
- {
- char arr[] = "zhangsan";
- Cstudent* XX = new Cstudent(arr, 'F', 18, 1001);
- Cstudent* CC = new Cstudent(arr, 'F', 18, 1001);
-
- CC->print_f();
- XX->print_f();
-
- if(CC == XX)
- {
- cout << "==" << endl;
- }
- else
- {
- cout << "!=" << endl;
- }
-
- return 0;
- }
复制代码
不知道什么地方出现了问题,求帮助!
16行改成 name = new char[longx];
18行、56行要使用strcpy_s
24行改成 delete []name;
- #include <iostream>
- #include <Cstring>
- using namespace std;
- class Cstudent
- {
- private:
- char* name;
- char sex;
- int num;
- int age;
- public:
- Cstudent(char* p_name, char p_sex, int p_num, int p_age) : sex(p_sex), num(p_num), age(p_age)
- {
- int longx = strlen(p_name) + 1;
- name = new char[longx] { 0 };
- strcpy_s(name, longx, p_name);
- }
- ~Cstudent()
- {
- if (name)
- {
- delete[]name;
- name = NULL;
- }
- }
- bool operator==(const Cstudent* stud);
- Cstudent& operator=(const Cstudent* stud);
- void print_f()
- {
- cout << name << "--" << sex << "--" << num << "--" << age << endl;
- }
- };
- bool Cstudent:: operator==(const Cstudent* stud)
- {
- int count = strcmp(this->name, stud->name);
- if (count == 0 && (this->sex == stud->sex) &&
- (this->num == stud->num) && (this->age == stud->age))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- Cstudent& Cstudent::operator=(const Cstudent* stud)
- {
- int n_len = 0;
- n_len = strlen(stud->name);
- name = new char[n_len + 1]{ 0 };
- strcpy_s(name, n_len, stud->name);
- num = stud->num;
- age = stud->age;
- sex = stud->sex;
- return *this;
- }
- int main()
- {
- char arr[] = "zhangsan";
- Cstudent* XX = new Cstudent(arr, 'F', 18, 1001);
- Cstudent* CC = new Cstudent(arr, 'F', 18, 1001);
- CC->print_f();
- XX->print_f();
- if (CC == XX)
- {
- cout << "==" << endl;
- }
- else
- {
- cout << "!=" << endl;
- }
- return 0;
- }
复制代码
|
上一篇: vs2919下一篇: 基础班,菜单工具栏美化CTrueColorToolBar类,转换到vs2010出问题
|