为什么在主函数中创建一个对象a里面的wori会报错?
class Employee {
private:
char Name;
char Adress;
int Zip;
public:
Employee(char name, char adress, int zip);
void change_name();
char getName()const { return Name; }
char getAdress()const { return Adress; }
char getZip()const { return Zip; }
void display() {
cout << "姓名:" << Name << endl;
cout << "街道地址:" << Adress << endl;
cout << "邮编:" << Zip << endl;
}
};
Employee::Employee(char name, char adress, int zip) { Name=name; Adress=adress; Zip=zip; }
void Employee::change_name() {
cout << "enter newname:" << endl;
cin >> Name;
cout << "change name:" << Name << endl;
}
int main()
{
Employee a("wori", "ca", "361026");
return 0;
}
char Name;定义了一个字符变量,只能存储一个字符。你应该定义一个字符数组char Name[20];这样就不会报错。对于char Adress;也需要定义字符数组
class Employee {
private:
char Name[20];
char Adress[20];
int Zip;
public:
Employee(char name[20], char adress[20], int zip[20]);
void change_name();
char getName()const { return Name[20]; }
char getAdress()const { return Adress[20]; }
char getZip()const { return Zip; }
void display() {
cout << "姓名:" << Name[20] << endl;
cout << "街道地址:" << Adress[20] << endl;
cout << "邮编:" << Zip << endl;
}
};
Employee::Employee(char name[20], char adress[20], int zip) { Name[20]=name[20]; Adress[20]=adress[20]; Zip=zip; }
void Employee::change_name() {
cout << "enter newname:" << endl;
cin >> Name;
cout << "change name:" << Name << endl;
}
给你改好了,请分清楚字符和字符串,还有在设置整型数时候别写字符串:
class Employee {
private:
char Name[16];
char Adress[16];
int Zip;
public:
Employee(char* name, char* adress, int zip);
void change_name();
char* getName() { return Name; }
char* getAdress() { return Adress; }
char getZip()const { return Zip; }
void display() {
cout << "姓名:" << Name << endl;
cout << "街道地址:" << Adress << endl;
cout << "邮编:" << Zip << endl;
}
};
Employee::Employee(char* name, char* adress, int zip) { strcpy(Name, name); strcpy(Adress, adress); Zip = zip; }
void Employee::change_name() {
cout << "enter newname:" << endl;
cin >> Name;
cout << "change name:" << Name << endl;
}
int main()
{
Employee a("wori", "ca", 361026);
return 0;
}
char name是一个字符
char name[]才是一个字符串
char 只是一个 字符,只能存储一个 char,需要定义 一个数组
class Employee {
private:
char Name[20];
char Adress[20];
int Zip;
public:
Employee(char* name, char* adress, int zip);
void change_name();
char* getName() { return Name; }
char* getAdress() { return Adress; }
char getZip()const { return Zip; }
void display() {
cout << "姓名:" << Name << endl;
cout << "街道地址:" << Adress << endl;
cout << "邮编:" << Zip << endl;
}
};
Employee::Employee(char* name, char* adress, int zip) { strcpy(Name, name); strcpy(Adress, adress); Zip = zip; }
void Employee::change_name() {
cout << "enter newname:" << endl;
cin >> Name;
cout << "change name:" << Name << endl;
}
int main()
{
Employee a("wori", "ca", 361026);
return 0;
}
将构造函数的参数类型改为const char *
注意字符和字符串的区别
构造函数第一的字符传递的是字符串
定义成string、类型
#include
#include
using namespace std;
class Employee {
private:
string Name;
string Adress;
int Zip;
public:
Employee(string name, string adress, int zip);
void change_name();
string getName()const { return Name; }
string getAdress()const { return Adress; }
int getZip()const { return Zip; }
void display() {
cout << "姓名:" << Name << endl;
cout << "街道地址:" << Adress << endl;
cout << "邮编:" << Zip << endl;
}
};
Employee::Employee(string name, string adress, int zip) { Name=name; Adress=adress; Zip=zip; }
void Employee::change_name() {
cout << "enter newname:" << endl;
getline(cin,Name);
cout << "change name:" << Name << endl;
}
int main()
{
Employee a("wori", "ca", 361026 );
return 0;
}
将类中变量的定义里的char改成string
然后要头部要记得include
#include <string>
using namespace