-1
#include <iostream>
using namespace std;

class S;

class R {
        int width, height;
        public:
        int area ()   // Area of rectangle
   {return (width * height);}
    void convert (S a);  
};
class S {
   private:
   int side;
   public:
   S (int a) : side(a) {}
   friend void convert(S a);

};

void R::convert (S a) {    
   width = a.side;
   height = a.side;    // Interpreting Square as an rectangle
}
int main () {

   int x;

   cin >> x;
   R rect;
   S sqr (x);
   rect.convert(sqr);
   cout << rect.area();
   return 0;
}

I am getting the following errors:

prog.cpp: In member function ‘void R::convert(S)’: prog.cpp:26:14: error: ‘int S::side’ is private within this context width = a.side; ^~~~ prog.cpp:16:8: note: declared private here int side; ^~~~ prog.cpp:27:15: error: ‘int S::side’ is private within this context height = a.side; // Interpreting Square as an rectangle ^~~~ prog.cpp:16:8: note: declared private here int side; ^~~~

I tried putting the friend function function private also but same error. Please help

2 Answers2

0

in class S you should have friend R;

friend void convert(S a); is meaningless because the compiler doesn't even know convert belongs to R not S.

AlexG
  • 1,091
  • 7
  • 15
0

For starters the name S is not declared before it is first used in the declaration

void convert ( S a);  

And secondly you have to specify that the function convert is a member function of the class R.

Try the following

class R {
        int width, height;
        public:
        int area ()   // Area of rectangle
   {return (width * height);}
    void convert ( class S a);  
                   ^^^^^^
};
class S {
   private:
   int side;
   public:
   S (int a) : side(a) {}
   friend void R::convert(S a);
              ^^^ 
};
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335