c++是出现了 error C2143: syntax error : missing ';' before 'using'错误,大神怎么办
#include<iostream>usingnamespacestd;#ifndefITEM_BASE#defineITEM_BASEclassperson{publi...
#include<iostream>
using namespace std;
#ifndef ITEM_BASE
#define ITEM_BASE
class person{
public:
person();
person(string, string, string);
string getName() const;
string getSSN() const;
string getAddress() const;
void setName(string );
void setSSN(string);
void setAddress(string);
void print();
person& operator=(const person&);
private:
string Name;
string SSN;
string address;
};
#endif
#include"person.h"
#include<iostream>
using namespace std;
person::person(){
Name="no name yet";
SSN="no ssn yet";
address="no address yet";
}
person::person(string name, string ssn, string add){
Name=name;
SSN=ssn;
address=add;
}
string person::getName() const{
return Name;
}
string person::getSSN() const{
return SSN;
}
string person::getAddress() const{
return address;
}
void person::setName(string name){
Name=name;
}
void person::setSSN(string ssn){
SSN=ssn;
}
void person::setAddress(string add){
address=add;
}
void person::print(){
string owner=getName();
string ssn=getSSN();
string add=getAddress();
cout<<"Owner:"<<owner.c_str()<<endl;
cout<<"SSN:"<<ssn.c_str()<<endl;
cout<<"Address:"<<add.c_str()<<endl;
}
person & person::operator=(const person &p){
person *per = new person();
per->setName(p.getName());
per->setSSN(p.getSSN());
per->setAddress(p.getAddress());
return *per;
} 展开
using namespace std;
#ifndef ITEM_BASE
#define ITEM_BASE
class person{
public:
person();
person(string, string, string);
string getName() const;
string getSSN() const;
string getAddress() const;
void setName(string );
void setSSN(string);
void setAddress(string);
void print();
person& operator=(const person&);
private:
string Name;
string SSN;
string address;
};
#endif
#include"person.h"
#include<iostream>
using namespace std;
person::person(){
Name="no name yet";
SSN="no ssn yet";
address="no address yet";
}
person::person(string name, string ssn, string add){
Name=name;
SSN=ssn;
address=add;
}
string person::getName() const{
return Name;
}
string person::getSSN() const{
return SSN;
}
string person::getAddress() const{
return address;
}
void person::setName(string name){
Name=name;
}
void person::setSSN(string ssn){
SSN=ssn;
}
void person::setAddress(string add){
address=add;
}
void person::print(){
string owner=getName();
string ssn=getSSN();
string add=getAddress();
cout<<"Owner:"<<owner.c_str()<<endl;
cout<<"SSN:"<<ssn.c_str()<<endl;
cout<<"Address:"<<add.c_str()<<endl;
}
person & person::operator=(const person &p){
person *per = new person();
per->setName(p.getName());
per->setSSN(p.getSSN());
per->setAddress(p.getAddress());
return *per;
} 展开
2个回答
展开全部
代码没有问题。只是上面的代码要分开到2个文件里面,一个person.h,内容如下
#include<iostream>
using namespace std;
#ifndef ITEM_BASE
#define ITEM_BASE
class person{
public:
person();
person(string, string, string);
string getName() const;
string getSSN() const;
string getAddress() const;
void setName(string );
void setSSN(string);
void setAddress(string);
void print();
person& operator=(const person&);
private:
string Name;
string SSN;
string address;
};
#endif
一个person.cpp,内容如下:
#include"person.h"
#include<iostream>
using namespace std;
person::person(){
Name="no name yet";
SSN="no ssn yet";
address="no address yet";
}
person::person(string name, string ssn, string add){
Name=name;
SSN=ssn;
address=add;
}
string person::getName() const{
return Name;
}
string person::getSSN() const{
return SSN;
}
string person::getAddress() const{
return address;
}
void person::setName(string name){
Name=name;
}
void person::setSSN(string ssn){
SSN=ssn;
}
void person::setAddress(string add){
address=add;
}
void person::print(){
string owner=getName();
string ssn=getSSN();
string add=getAddress();
cout<<"Owner:"<<owner.c_str()<<endl;
cout<<"SSN:"<<ssn.c_str()<<endl;
cout<<"Address:"<<add.c_str()<<endl;
}
person & person::operator=(const person &p){
person *per = new person();
per->setName(p.getName());
per->setSSN(p.getSSN());
per->setAddress(p.getAddress());
return *per;
}
还需要一个main函数,来生成person类的对象并调用里面的方法。如下:
#include<iostream>
#include "person.h"
int main()
{
person *p = new person();
p->setName("shaolei");
p->setSSN("8-4-303");
p->setAddress("Hangzhou Zhejiang");
p->print();
system("pause");
return 0;
}
追问
谢谢
展开全部
#ifndef ITEM_BASE
#define ITEM_BASE
#include<iostream>
using namespace std;
class person{
public:
person();
person(string, string, string);
string getName() const;
string getSSN() const;
string getAddress() const;
void setName(string );
void setSSN(string);
void setAddress(string);
void print();
person& operator=(const person&);
private:
string Name;string SSN;string address;
};
#endif
-----------------------------------------------------------------------------
#include<iostream>
#include "person.h"
person::person(){
Name="no name yet"; SSN="no ssn yet"; address="no address yet";
}
person::person(string name, string ssn, string add)
{
Name=name; SSN=ssn; address=add;
}
string person::getName() const{ return Name;}
string person::getSSN() const{ return SSN;}
string person::getAddress() const{ return address;}
void person::setName(string name){ Name=name;}
void person::setSSN(string ssn){ SSN=ssn;}
void person::setAddress(string add){ address=add;}
void person::print(){
string owner=getName();
string ssn=getSSN();
string add=getAddress();
cout<<"Owner:"<<owner.c_str()<<endl;
cout<<"SSN:"<<ssn.c_str()<<endl;
cout<<"Address:"<<add.c_str()<<endl;
}
person & person::operator=(const person &p){
person *per = new person();
per->setName(p.getName());
per->setSSN(p.getSSN());
per->setAddress(p.getAddress());
return *per;
}
int main()
{
person p;
return 0;
}
#define ITEM_BASE
#include<iostream>
using namespace std;
class person{
public:
person();
person(string, string, string);
string getName() const;
string getSSN() const;
string getAddress() const;
void setName(string );
void setSSN(string);
void setAddress(string);
void print();
person& operator=(const person&);
private:
string Name;string SSN;string address;
};
#endif
-----------------------------------------------------------------------------
#include<iostream>
#include "person.h"
person::person(){
Name="no name yet"; SSN="no ssn yet"; address="no address yet";
}
person::person(string name, string ssn, string add)
{
Name=name; SSN=ssn; address=add;
}
string person::getName() const{ return Name;}
string person::getSSN() const{ return SSN;}
string person::getAddress() const{ return address;}
void person::setName(string name){ Name=name;}
void person::setSSN(string ssn){ SSN=ssn;}
void person::setAddress(string add){ address=add;}
void person::print(){
string owner=getName();
string ssn=getSSN();
string add=getAddress();
cout<<"Owner:"<<owner.c_str()<<endl;
cout<<"SSN:"<<ssn.c_str()<<endl;
cout<<"Address:"<<add.c_str()<<endl;
}
person & person::operator=(const person &p){
person *per = new person();
per->setName(p.getName());
per->setSSN(p.getSSN());
per->setAddress(p.getAddress());
return *per;
}
int main()
{
person p;
return 0;
}
追问
我这个是哪里错了呢
追答
调整了一下顺序,你可以参考
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询