Friend function should be a public function, if it is in a class.
来源:互联网 发布:nemo阿拉伯语软件 编辑:程序博客网 时间:2024/06/11 13:38
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.
- Friend function should be a public function, if it is in a class.
- Class"X" is public, should be declared in a file named "X.java"
- The "data" option should be a function that returns a per-instance value in component definitions.
- When executing a query what a callback function should be
- Given an integer, write a function to determine if it is a power of two.
- Given an integer, write a function to determine if it is a power of two
- The callback function, ewew, specified in the UIR file, is not a known function. If you are using a
- $ is not a function
- $ is not a function
- is not a function
- friend class & function
- expreiment to see what a callback function should be When executing a query
- 71.Which arithmetic operations can be performed on a column by using a SQL function that is built in
- friend function in C++
- Element type is invalid: expected a string (for built-in components) or a class/function (for compos
- Friend class or function and nested types in class templates
- 2.7 Implement a function to check if a linked list is a palindrome
- static member variable and static member function in a class
- Windows 消息空间
- exer
- OCI-22053: overflow error的异常
- 谈谈我曾误解过的两条编程格言.
- heritrix 下载、安装、配置、以及简单开发 编辑
- Friend function should be a public function, if it is in a class.
- 有用的awk脚本
- 在win7(64位)中填加网络打印机(hp2015)
- 新人如何适应新工作环境
- 新浪微博 SDK for Windows Phone 7 (API)
- VS2008下OpenCV 2.3环境的搭建
- SqlHelper类
- oracle 触发器
- 聊胜于无 Java之Caesar与Vigenere实现