#include <iostream> #include <string> using namespace std; int main() { class Person { string name; string address; const string& getName() const { return name; } const string& getAddress() const { return address; } }; istream& read(istream& is, Person& person) { is >> person.name >> person.address; if (!is) person = Person(); return is; } ostream& print(ostream &os, const Person& person) { os << person.name << " " << person.address; return os; } return 0; }
编译出错,是什么原因啊,求指导释
解决方案
40
1.C/C++不允许在函数体内再定义函数
2.name和address是private的,不能通过.直接访问
2.name和address是private的,不能通过.直接访问
#include <iostream> #include <string> using namespace std; class Person { public: string name; string address; const string& getName() const { return name; } const string& getAddress() const { return address; } }; istream& read(istream& is, Person& person) { is >> person.name >> person.address; if (!is) person = Person(); return is; } ostream& print(ostream &os, const Person& person) { os << person.name << " " << person.address; return os; } int main() { return 0; }