[英]C++ objects as class members
making object from Address and trying to refer to it is not working 从地址创建对象并尝试引用它不起作用
Student::Student(string studentInfo_c){ // student constructor
stringstream ss(studentInfo_c);
getline(ss, lastName, ',');
getline(ss, firstName, ',');
getline(ss, address1, ',');
getline(ss, address2, ',');
getline(ss, city, ',');
getline(ss, state, ',');
getline(ss, zipCode, ',');
Address sAddrs(address1, address2, city, state, zipCode);
}
ostream& operator<<(ostream& os, const Student& s){ os << s.lastName << ", " << s.firstName << " " << s.aAddrs;
return os; // first place that sAddrs oject is referenced
}
class prototypes: 类原型:
class Student {
private:
string line;
string lastName;
string firstName;
string address1;
string address2;
string city;
string state;
string zipCode;
public:
//Student() : Address aAddrs this didnt work...
Student(string studentInfo_c);
string get_firstName();
string get_lastName();
void set_address(string address1_f, string address2_f, string city_f, string state_f, string zipCode_f);
friend ostream& operator<<(ostream& os, const Student& s);
~Student();
} }
error: In function 'std::ostream& operator<<(std::ostream&, const Student&)':| 错误:函数'std :: ostream&运算符<<(std :: ostream&,const Student&)':| C:\\Users\\Chris\\Documents\\Summer 2017 Semesters\\HeapOStudents\\student.cpp|67|error: 'const class Student' has no member named 'aAddrs'|
C:\\ Users \\ Chris \\ Documents \\ 2017年夏季学期\\ HeapOStudents \\ student.cpp | 67 |错误:“ const class Student”没有名为“ aAddrs”的成员|
C:\\Users\\Chris\\Documents\\Summer 2017 Semesters\\df\\student.cpp|73|error: 'aAddrs' was not declared in this scope| C:\\ Users \\ Chris \\ Documents \\ Sumesters \\ df \\ student.cpp | 73 |错误|错误:在此范围内未声明“ aAddrs” |
||=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| || ===构建失败:6个错误,0个警告(0分钟,0秒)=== |
ps I know this is similar to other questions but none of them have seemed to work for me, they are slightly more advanced. ps:我知道这与其他问题相似,但是似乎没有一个问题对我有用,它们稍微更高级了。
thanks, 谢谢,
According to recommendations in comments, I assembled an MCVE to show how it could be done: 根据评论中的建议,我组装了一个MCVE来展示如何实现:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
class Address {
private:
string address1;
string address2;
string city;
string state;
string zipCode;
public:
// default constructor (leaving contents empty)
Address() { }
// constructor.
Address(
const string &address1, const string &address2,
const string &city, const string &state,
const string &zipCode):
address1(address1), address2(address2),
city(city), state(state), zipCode(zipCode)
{ }
friend ostream& operator<<(ostream& os, const Address &a);
};
ostream& operator<<(ostream& os, const Address &a)
{
return os
<< " Address 1: " << a.address1 << endl
<< " Address 2: " << a.address2 << endl
<< " City : " << a.city << endl
<< " State : " << a.state << endl
<< " Zip Code : " << a.zipCode << endl;
}
class Student {
private:
string lastName;
string firstName;
Address sAddrs;
public:
// constructor.
Student(const string &studentInfo_c);
friend ostream& operator<<(ostream& os, const Student &s);
};
Student::Student(const string &studentInfo_c)
// all members are default constructed (leaving them empty)
{
stringstream ss(studentInfo_c);
getline(ss, lastName, ',');
getline(ss, firstName, ',');
string address1, address2, city, state, zipCode;
getline(ss, address1, ',');
getline(ss, address2, ',');
getline(ss, city, ',');
getline(ss, state, ',');
getline(ss, zipCode, ',');
sAddrs = Address(address1, address2, city, state, zipCode);
}
ostream& operator<<(ostream &os, const Student &s)
{
return os
<< "Student " << s.lastName << ", " << s.firstName << endl
<< "Address: " << endl
<< s.sAddrs << endl;
}
int main()
{
string sample("Doe,John,1 Anyway,,Anytown,Anystate,12345,");
Student s(sample);
cout << s;
return 0;
}
Tested with g++: 用g ++测试:
$ g++ -std=c++11 -o test test.cc
$ ./test
Student Doe, John
Address:
Address 1: 1 Anyway
Address 2:
City : Anytown
State : Anystate
Zip Code : 12345
$
Notes: 笔记:
Address
provides two constructors: a default constructor and a second one for initialization. Address
提供了两个构造函数:一个默认构造函数和另一个用于初始化的构造函数。
Student::Student
constructs all members with default constructors. Student::Student
使用默认构造函数构造所有成员。 (Therefore, Address
has to provide one.) (因此,
Address
必须提供一个。)
In the body of Student::Student
, a temporary instance of Address
is created and assigned to Student::sAddrs
. 在
Student::Student
的正文中,创建了Address
的临时实例并将其分配给Student::sAddrs
。 This works because the assignment operator ( Address& Address::operator=(const Address&)
) is generated by the compiler. 这是有效的,因为赋值运算符(
Address& Address::operator=(const Address&)
)是由编译器生成的。 (This is not the most efficient code but the one with least source code effort.) (这不是最有效的代码,而是最少的源代码工作。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.