成功创建c ++类和对象[重复]
Posted
技术标签:
【中文标题】成功创建c ++类和对象[重复]【英文标题】:Creating c++ classes and objects successfully [duplicate] 【发布时间】:2019-11-19 21:32:16 【问题描述】:我正在从头开始用 C++ 编写应用程序,从一开始我就在定义类、为其创建构造函数以及创建类的实例/对象时遇到了麻烦。我在编写代码时会检查代码以避免错误,但是当我尝试创建类的对象时,我的问题就开始了。我会感谢任何人的帮助。也可能是我以错误的方式在 C++ 中创建类、构造函数等。我将不胜感激修改我的代码和任何建议。 当我运行下面的代码时,我收到错误消息“运算符
#include <string>
using namespace std;
class Person
public:
Person(string firstName, string lastName, string nationality, string occupation);
Person();
void setFirstName(string firstName)
this->firstName = firstName;
void getFirstName()
cout << firstName;
void setLastName(string lastName)
this->lastName = lastName;
void getLastName()
cout << lastName;
void setNationality(string nationality)
this->nationality = nationality;
void getNationality()
cout << nationality;
void setOccupation(string occupation)
this->occupation = occupation;
void getOccupation()
cout << occupation;
public:
string firstName;
string lastName;
string nationality;
string occupation;
;
int main()
Person p3;
p3.setFirstName("Larra");
cout << p3.getFirstName();
cout << p3.firstName;
return 0;
我已将代码更改为以下代码。但是我现在收到的错误消息是'collect2:error:Id returned 1 exit status'。请问我该如何解决?
#include <iostream>
#include <string>
using namespace std;
class Person
public:
Person(string firstName, string lastName, string nationality, string occupation);
Person();
void setFirstName(string firstName)
this->firstName = firstName;
string getFirstName()
return firstName;
void setLastName(string lastName)
this->lastName = lastName;
string getLastName()
return lastName;
void setNationality(string nationality)
this->nationality = nationality;
string getNationality()
return nationality;
void setOccupation(string occupation)
this->occupation = occupation;
string getOccupation()
return occupation;
public:
string firstName;
string lastName;
string nationality;
string occupation;
;
int main()
Person p3;
p3.setFirstName("Larra");
cout << p3.firstName;
return 0;
```
******************************************************************
I HAVE FINALLY GOT IT WORKING. THANKS TO EVERYONE THAT HELPED. I commented out two lines around the start of the program and it started working. You can see the working version below.
******************************************************************
#include <iostream>
#include <string>
using namespace std;
class Person
public:
string firstName;
string lastName;
string nationality;
string occupation;
public:
//Person(string firstName, string lastName, string nationality, string occupation);
//Person();
void setFirstName(string firstName)
this->firstName = firstName;
string getFirstName()
return firstName;
void setLastName(string lastName)
this->lastName = lastName;
string getLastName()
return lastName;
void setNationality(string nationality)
this->nationality = nationality;
string getNationality()
return nationality;
void setOccupation(string occupation)
this->occupation = occupation;
string getOccupation()
return occupation;
;
int main()
Person p3;
p3.setFirstName("Larra");
p3.setLastName("Marrudds");
cout << p3.firstName << '\n' << p3.lastName <<endl;
return 0;
【问题讨论】:
哪些行有错误? 投票结束是一个错字。你忘了#include <iostream>
错误来自 cout
getFirstName
没有返回任何内容,实际上没有任何 get
方法。
@ayedecoder 你有getFirstName()
返回void
。它应该是字符串。 void
不是您可以打印的。
【参考方案1】:
重载类的构造函数和<<
ostream 的运算符。这将允许您使用运算符
Person() = default;
然后有一个替代的集合函数或另一个构造函数。
对于你的成员变量国籍和职业,你的参数也是一样的。它们必须不同。
#include <iostream>
#include <ostream>
#include <string>
using namespace std;
class Person
public:
string firstName;
string lastName;
string Nationality;
string Occupation;
Person() = default; // Overload the constructor
Person(string firstName, string lastName, string n, string o)
// You could use this also,
this->firstName = firstName;
this->lastName = lastName;
this->Nationality = n;
this->Occupation= o;
// etc...
;
void setFirstName(string firstName)
this->firstName = firstName;
void getFirstName()
cout << firstName;
void setLastName(string lastName)
this->lastName = lastName;
void getLastName()
cout << lastName;
void setNationality(string nationality)
this->Nationality = nationality;
void getNationality()
cout << Nationality;
void setOccupation(string occupation)
this->Occupation = occupation;
void getOccupation()
cout << Occupation;
// Overload operator
friend std::ostream& operator<<(std::ostream &os, Person &p)
os << p.firstName << " " << p.lastName << " " << p.Nationality << " " << p.Occupation;
return os;
;
int main()
Person p3;
Person p4("Jack", "Hanson", "Nationality", "Ocupation");
p3.setFirstName("Larra");
p3.setLastName ("Hanson");
cout << p3 << endl;
cout << p4 << endl;
return 0;
【讨论】:
谢谢大家。我现在已经解决了这个问题,我已经用工作代码编辑了我的原始帖子。以上是关于成功创建c ++类和对象[重复]的主要内容,如果未能解决你的问题,请参考以下文章