求大神帮小白(比较简单)我看下代码 C++ 实在看不出问题了 谢谢
题目:
设计一个学校在册人员类(Person)。数据成员包括:身份证号(IdPerson),姓名(Name),性别(Sex),生日(Birthday)和家庭住址(HomeAddress)。成员函数包括人员信息的录入和显示。还包括构造函数与拷贝构造函数。设计一个合适的初始值
#include <iostream>
#include <string>using namespace std;
class Person{
private:
string IdPerson;
string Name;
string Sex;
string Birthday;
string HomeAddress;
public:
Person();
~Person(){}//析构函数 注意:一个类中只有一个析构函数,用于结束对象
Person(string,string,string,string,string,string);
Person(Person&);
string GetIdPerson();
string GetName();
string GetSex();
string GetBirthday();
string GetHomeAddress();
};
Person(){
IdPerson='0'; Name='0'; Sex='0';
Birthday='0'; HomeAddress='0';
}//构造函数默认值
Person(Person p1){
IdPerson=p.IdPerson; Name=p.Name; Sex=p.Sex;
Birthday=p.Birthday; HomeAdress=p.HomeAddress;
}//复制构造函数
Person(string id,string name,string sex,string bir,string hd){
IdPerson=id; Name=name; Sex=sex;
Birthday=bir; HomeAdress=hd;
}//带参构造函数初始化
Person(Person & p2){
IdPerson=p.IdPerson; Name=p.Name; Sex=p.Sex;
Birthday=p.Birthday; HomeAdress=p.HomeAddress;
}//复制构造函数
string Person::GetName(){
return Name;
}
string Person::GetIdPerson(){
return IdPerson;
}
string Person::GetSex(){
return Sex;
}
string Person::GetBirthday(){
return Birthday;
}
string Person::GetHomeAddress(){
return HomeAddress;
}
void main(){
string a,b,c,d,e;
cout<<"默认构造函数下的、"<<endl;
Person p1;
cout<<"身份证号码:"<<p1.GetIdPerson()<<"n"<<"姓名:"<<p1.GetName()<<"n"
<<"性别:"<<p1.GetSex()<<"n"<<"生日:"<<p1.GetBirthday()<<"n"
<<"家庭住址:"<<p1.GetHomeAddress()<<endl;
cout<<"请输入身份证号码:"<<endl;
cin>>a;
cout<<"请输入姓名:"<<endl;
cin>>b;
cout<<"请输入性别:"<<endl;
cin>>c;
cout<<"请输入生日:"<<endl;
cin>>d;
cout<<"请输入家庭地址:"<<endl;
cin>>e;
Person p2(a,b,c,d,e);
cout<<"身份证号码:"<<p1.GetIdPerson()<<"n"<<"姓名:"<<p1.GetName()<<"n"
<<"性别:"<<p1.GetSex()<<"n"<<"生日:"<<p1.GetBirthday()<<"n"
<<"家庭住址:"<<p1.GetHomeAddress()<<endl;
}
错误提示:
:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(25) : error C2065: 'IdPerson' : undeclared identifier
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(25) : error C2065: 'Name' : undeclared identifier
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(25) : error C2065: 'Sex' : undeclared identifier
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(26) : error C2065: 'Birthday' : undeclared identifier
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(26) : error C2065: 'HomeAddress' : undeclared identifier
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(27) : warning C4508: 'Person' : function should return a value; 'void' return type assumed
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(29) : error C2146: syntax error : missing ')' before identifier 'p1'
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(29) : error C2501: 'Person' : missing storage-class or type specifiers
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(29) : error C2373: 'Person' : redefinition; different type modifiers
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(24) : see declaration of 'Person'
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(29) : error C2440: 'initializing' : cannot convert from 'int (__cdecl *)(void)' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(29) : error C2059: syntax error : ')'
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(29) : error C2143: syntax error : missing ';' before '{'
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(29) : error C2447: missing function header (old-style formal list?)
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(35) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
(or there is no acceptable conversion)
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(35) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
(or there is no acceptable conversion)
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(35) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
(or there is no acceptable conversion)
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(36) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
(or there is no acceptable conversion)
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(36) : error C2065: 'HomeAdress' : undeclared identifier
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(36) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
(or there is no acceptable conversion)
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(37) : warning C4508: 'Person' : function should return a value; 'void' return type assumed
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(39) : error C2065: 'p2' : undeclared identifier
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(39) : error C2563: mismatch in formal parameter list
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(39) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(63) : error C2146: syntax error : missing ';' before identifier 'p1'
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(63) : warning C4551: function call missing argument list
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(63) : error C2065: 'p1' : undeclared identifier
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(64) : error C2228: left of '.GetIdPerson' must have class/struct/union type
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(64) : error C2228: left of '.GetName' must have class/struct/union type
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(65) : error C2228: left of '.GetSex' must have class/struct/union type
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(65) : error C2228: left of '.GetBirthday' must have class/struct/union type
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(66) : error C2228: left of '.GetHomeAddress' must have class/struct/union type
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(77) : error C2146: syntax error : missing ';' before identifier 'p2'
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(77) : warning C4551: function call missing argument list
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(78) : error C2228: left of '.GetIdPerson' must have class/struct/union type
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(78) : error C2228: left of '.GetName' must have class/struct/union type
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(79) : error C2228: left of '.GetSex' must have class/struct/union type
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(79) : error C2228: left of '.GetBirthday' must have class/struct/union type
C:应用软件C++Microsoft Visual StudioMyProjectsC_study33.cpp(80) : error C2228: left of '.GetHomeAddress' must have class/struct/union type
执行 cl.exe 时出错.
3.obj - 1 error(s), 0 warning(s)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
大哥,定义类的成员方法不在class里面请加类名class::function 比如person::getpersonid
Person(Person p1)这样声明是不合法的,会导致循环调用复制构造函数
回复
不合法就不要用嘛
回复
谢谢 我懂了 周六日这两天忙其他事情了 没来得及看 谢谢
好好看看书吧。
第一,看下类外定义类成员函数的方式(主要问题所在),后面的都能写对,前面咋就写不对呢
第二,Person(Person p1)这条你声明了吗??
为什么那样就多声明了一个参数 不太懂 能不能解释一下 谢谢
回复
你仔细数一下你的声明(Person(string,string,string,string,string,string); )和你定义(Person(string id,string name,string sex,string bir,string hd))的参数个数嘛,写程序还是要细心一点,这些错误其实都不是很难,看看错误提示找一找源程序或者在网上查一查
1. HomeAddress有几处拼写错了
2. 类外定义函数时,使用“类名::函数名”的形式(几个构造函数就是因为这个原因编译错误)
3. 删除Person(Person p1)函数
4. Person(string id,string name,string sex,string bir,string hd)的声明是错误的,声明多了一个参数
5. 打印输出的是中文,要使用GBK来保存源文件