In the book of Herbert Shildt the following example is used:

#include <iostream> using namespace std; const int IDLE=0; const int INUSE=1; class С2; // опережающее объявление class C1 {   int status; // IDLE если сообщение неактивно, INUSE если сообщение выведено на экран.   // ...  public:   void set_status(int state);   friend int idle(C1 a, C2 b); }; class C2 {   int status; // IDLE если сообщение неактивно, INUSE если сообщение выведено на экран.   // ...  public:   void set_status(int state);   friend int idle(C1 a, C2 b); }; void C1::set_status(int state) {  status = state; } void C2::set_status(int state) {  status = state; } // Функция idle() - "друг" для классов C1 и C2. int idle(C1 a, C2 b) {  if(a.status || b.status) return 0;  else return 1; } int main() {  C1 x;  C2 y;  x.set_status(IDLE);  y.set_status(IDLE);  if(idle(x, y)) cout << "Экран свободен.\n";  else cout << "Отображается сообщение.\n";  x.set_status(INUSE);  if(idle(x, y)) cout << "Экран свободен.\n";  else cout << "Отображается сообщение.\n";  return 0; } 

The compiler reports that it is impossible to access the private member declared in class C1, although the idle function is declared friendly. The error is produced in the definition of the idle function in this line of status near a. is highlighted in red:

 if (a.status || b.status) return 0; 

What is the error and how to fix it?

  • 2
    It was worth replacing the Russian C with the Latin one in the preliminary announcement of C2 , how it all worked: ideone.com/z1RV9j - Harry
  • I guessed that this could be, but everything seemed to be able to change, changed, but it did not) - Vlad Kvochin
  • In any case, "highlighted in red" - is not a reason to judge the presence of an error. It is necessary to compile and look at the compiler messages. - AnT

0