求教编程题关于c++ 输出重载
1 #include <iostream>2 #include <cstring>
3 using namespace std;
4
5 class Girl{
6 string name;
7 int age;
8 bool hasLove;
9 public:
10 Girl()
11 {
12 cout << "Girl()" << endl;
13 }
14 Girl(string n, int a, bool h)
15 {
16 cout << "Girl(string, int, bool)" << endl;
17 name = n;
18 age = a;
19 hasLove = h;
20 }
21
22 friend ostream &operator<<(ostream&, Girl&);
23
24 void show() const
25 {
26 cout << name << ":" << age << ":" << (hasLove?"has":"not") << endl;
27 }
28
29 /*
30 friend ostream &operator<<(ostream &o, const Girl &g)
31 {
32 g.show();
33 return o;
34 }
35 */
36 };
37
38 ostream &operator<<(ostream &o, Girl &g)
39 {
40 o << Girl.name << Girl.age << Girl.hasLove;
41 return o;
42 }
43
44 int main()
45 {
46 Girl g("han", 22, false);
47 cout << g << endl;
48 g.show();
49 Girl g2;
50 g2.show();
51
52 return 0;
53 }
54
gcc报错
construct.cpp: In function ‘std::ostream& operator<<(std::ostream&, Girl&)’:
construct.cpp:40:11: error: expected primary-expression before ‘.’ token
construct.cpp:40:24: error: expected primary-expression before ‘.’ token
construct.cpp:40:36: error: expected primary-expression before ‘.’ token