How to allow "class should be used when declaring a friend"?

class two; class one { int a; public: one() { a = 8; } friend two; }; class two { public: two() { } two(one i) { cout << ia; } }; int main() { one o; two t(o); getch(); } 

I get this error from Dev-C ++:

 a class-key must be used when declaring a friend 

But it works great when compiling with Microsoft Visual C ++ compiler.

+4
source share
2 answers

You need

  friend class two; 

instead

  friend two; 

In addition, you do not need to forward the ads individually, because the friend declaration is itself a declaration. You can even do this:

 //no forward-declaration of two class one { friend class two; two* mem; }; class two{}; 
+12
source

Your code has:

 friend two; 

What should be:

 friend class two; 
+5
source

Source: https://habr.com/ru/post/1369367/


All Articles