Friend function should be a public function, if it is in a class.
2011-11-04 11:39
495 查看
See the code please.
JSC.h
JYJ.h
main.cpp
How about changing the friend function to a private member? See the result!
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.
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.
相关文章推荐
- android The public type classname must be defined in its own file 报错
- What is an abstract class, and when should it be used?
- A class file was not written. The project may be inconsistent, if so try refreshing this project and building it. eclipse提示错误
- A class file was not written. The project may be inconsistent, if so try refreshing this project and building it
- 调试记录:The public type <<classname>> must be defined in its own file
- About Derby "ERROR 42X51: The class '***' does not exist or is inaccessible. This can hap pen if the class is not public."
- 调试记录:The public type <<classname>> must be defined in its own file
- A class file was not written. The project may be inconsistent, if so try refreshing this project and building it. eclipse提示错误
- [经验]Class MediaTransCoder is implemented in both One of the two will be used. Which one is undefined
- The type xx cannot be resolved.It is indirectly referenced from required .class
- Visiting the Ikea stores can be very overwhelming even if it is not your first time. If it is your first time
- Request is not public in 'okhttp3.Request'.Cannot be accessed from outside package
- Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe
- App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app’s Info.plist file
- iOS:Class Foo is implemented in both MyApp and OtherApp. One of the two will be used. Which one is u
- The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files
- warning: the ‘gets' function is dangerous and should not be used
- The type XXX cannot be resolved.It is indirectly referenced from required .class
- 基于ZigBee的物联网家居系统学习-android端app的问题The type java.lang.Class cannot be resolved. It is indirectly refer
- The public type WebSocketTest must be defined in its own file