Guest User

Untitled

a guest
Jan 16th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include "b.h"
  2. namespace GLOBAL{
  3.  
  4. class A{
  5. public:
  6. void doSomething(B);
  7. }
  8. }
  9.  
  10. #include "a.h"
  11. using namespace GLOBAL;
  12.  
  13. void A::doSomething(B b){
  14.  
  15. b.use();
  16. }
  17.  
  18. namespace GLOBAL{
  19.  
  20. class B{
  21. public:
  22. friend void GLOBAL::A::doSomething(B);
  23. private:
  24. void use();
  25. }
  26.  
  27. ‘GLOBAL::A’ has not been declared
  28.  
  29. ‘void GLOBAL::B::use()’ is private
  30.  
  31. namespace GLOBAL
  32. {
  33. class B;
  34.  
  35. class A
  36. {
  37. public:
  38. void doSomething(B& b);
  39. };
  40. };
  41.  
  42.  
  43. namespace GLOBAL
  44. {
  45. class B
  46. {
  47. public:
  48. friend void GLOBAL::A::doSomething(B&);
  49. private:
  50. void use()
  51. {
  52. }
  53. };
  54. };
  55.  
  56. void GLOBAL::A::doSomething(B& b)
  57. {
  58. b.use();
  59. }
  60.  
  61. namespace GLOBAL
  62. {
  63. class B
  64. {
  65. public:
  66. friend class A;
  67. private:
  68. void use()
  69. {
  70. }
  71. };
  72. };
  73.  
  74. namespace GLOBAL
  75. {
  76. class A
  77. {
  78. public:
  79. void doSomething(B b);
  80. };
  81. };
  82.  
  83. void GLOBAL::A::doSomething(B b)
  84. {
  85. b.use();
  86. }
  87.  
  88. friend class A;
  89.  
  90. // #include "b.h" // remove this line it is not needed.
  91. namespace GLOBAL{
  92.  
  93. class B; // Forward declare the class here.
  94.  
  95. class A{
  96. public:
  97. void doSomething(B&); // Note: This should probably be a reference.
  98. // Change to doSomething(B&);
  99. }
  100. }
  101.  
  102. // Add this line it is needed for the friend declaration.
  103. // To have a member as a friend we need the definition of 'A'
  104. #include "a.h"
  105.  
  106. namespace GLOBAL{
  107.  
  108. class B{
  109. public:
  110. friend void GLOBAL::A::doSomething(B&);
  111. private:
  112. void use();
  113. }
  114.  
  115. #include "a.h"
  116. // Add this line to include the B definition so you know how to call use()
  117. #include "b.h"
  118. using namespace GLOBAL;
  119.  
  120. void A::doSomething(B& b){ // b should be a reference otherwise you are copying it.
  121.  
  122. b.use();
  123. }
Add Comment
Please, Sign In to add comment