C++编程问题请大神赐教谢谢! 5C

为什么在主函数中创建一个对象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;
}

11个回答

char Name;定义了一个字符变量,只能存储一个字符。你应该定义一个字符数组char Name[20];这样就不会报错。对于char Adress;也需要定义字符数组

qq_41517828
qq_41517828 改成这样还是报错
接近 2 年之前 回复

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;

}
gkzscs
gkzscs 你这是把int型也改了,int是对的,不用改成数组。前面两个char改成数组就可以了。
接近 2 年之前 回复

给你改好了,请分清楚字符和字符串,还有在设置整型数时候别写字符串:

 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;
}

共11条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐