Friend function should be a public function, if it is in a class.

See the code please.

JSC.h

#ifndef JSC_H_
#define JSC_H_

#include <iostream>
using namespace std;

class JYJ;
class JSC
{
public:
	JSC() {}
//private:
	void look(const JYJ& jyj);
};


#include "JYJ.h"

void JSC::look(const JYJ& jyj)
{
	 cout<<jyj.blove<<endl;
}
#endif /* JSC_H_ */

JYJ.h

#ifndef JYJ_H_
#define JYJ_H_

#include "JSC.h"

class JYJ
{
public:
	JYJ() { blove = true; }
	friend void JSC::look(const JYJ& jyj);
private:
	bool blove;
};

#endif /* JYJ_H_ */

main.cpp

#include "JSC.h"
#include "JYJ.h"

int main()
{
	JSC jsc;
	JYJ jyj;
	jsc.look(jyj);
	return 0;
}


How about changing the friend function to a private member? See the result!

JSC.h

#ifndef JSC_H_
#define JSC_H_

#include <iostream>
using namespace std;

class JYJ;
class JSC
{
public:
	JSC() {}
private:
	void look(const JYJ& jyj);
};


#include "JYJ.h"

void JSC::look(const JYJ& jyj)
{
	 cout<<jyj.blove<<endl;
}
#endif /* JSC_H_ */


It is clear to see the error. It is the same with "protected", only right in "public". Also, it is interesting to note the " #include " and " class " sequence in the coding.

Thanks to the comment. I should take your illustration to rephrase this issue. Yes, :: only decides the attribution to whom, whereas in order to let the class JYJ see the friend function, the one should be public only.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值