Problem
今天在完成一份C++作业时,遇到了一个问题:
已经将函数作为友元放在了Port类,在分文件编写时仍然提示无法访问类中的私有成员

Solve
在头文件中加入两行代码,这可能也是我的一个习惯问题,iostream只在方法文件中添加
port.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
   | #ifndef PORT_H #define PORT_H
  #include <iostream> //头文件中务必加入这两行代码 using namespace std;
  class Port { private: 	char* brand; 	char style[20]; 	int bottles; public: 	Port(const char* br = "none", const char* st = "none", int b = 0); 	Port(const Port& p); 	virtual ~Port() { delete[]brand; } 	Port& operator = (const Port& p); 	Port& operator += (int b); 	Port& operator-=(int b); 	int Bottlecount() const { return bottles; } 	virtual void show() const; 	friend ostream& operator<<(ostream& os, const Port& p); };
  #endif
   |