2

In this code , i have made max function of class B friend of class A.I have also done forward declaration of class B.But it is giving error.

#include<iostream>

using namespace std;

class B;

class A
{
   int a;
   public:

   void get()
   {
      cin>>a;
   }

   friend void B :: max(A , B);
};

class B
{
   int b;
   public:

   void get()
   {
      cin>>b;
   }

   void max(A x, B y)
   {
      if (x.a > y.b)
         cout<< " x is greater";
      else cout<<"y is greater";
   }
};

int main()
{
   A x;
   B y,c;
   x.get();
   y.get();
   c.max(x,y);
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
ishan bansal
  • 69
  • 1
  • 3
  • 7
  • 1
    **Always** post any error text your questioning **verbatim**. – WhozCraig Aug 23 '15 at 19:33
  • Did you see this question in your research: [Declare a member-function of a forward-declared class as friend](https://stackoverflow.com/questions/6310720/declare-a-member-function-of-a-forward-declared-class-as-friend) – WhozCraig Aug 23 '15 at 19:39
  • You can get some tips here: [How to forward declare a member function of a class to use in another class](http://stackoverflow.com/questions/11344195/how-to-forward-declare-a-member-function-of-a-class-to-use-in-another-class?rq=1) – Bo Persson Aug 23 '15 at 19:40

3 Answers3

2

As R Sahu already answered:

You cannot use:

friend void B :: max(A , B);

without the complete definition of B.

This is how you can achieve your goal:

#include<iostream>
using namespace std;

class A;

class B{
    int b = 2;

public: 
    void max(A x, B y);
};

class A{
    int a = 1;
public:
    friend void B :: max(A , B);
};

void B::max(A x, B y){
    if (x.a > y.b)
        cout<< " x is greater";
    else 
        cout<<"y is greater";
}

int main(){
A x;
B y,c;
c.max(x,y);
}
Elohim Meth
  • 1,777
  • 9
  • 13
2

B is incomplete at the point where you declare B::max as a friend method. Thus, compiler does not know if there is such a method.

This means that you need to

  1. reorder classes, so that A knows B has a method B::max and
  2. Implement the method B::max outside of the class definition, when both classes are complete, because you access internal variables.

It is also a good idea to pass your arguments by const reference. Use const to emphasise that you are not modifying them. Pass by reference to avoid unnecessary copying.

So, with this in mind:

class A;

class B{
    int b;
public: 
    void get(){
        cin>>b;
    }
    void max(const A& x, const B& y);
};

class A{
    int a;
public:
    void get(){
        cin>>a;
    }
    friend void B :: max(const A& , const B&);
};

void B::max(const A& x, const B& y) {
    if (x.a > y.b)
       cout<< " x is greater";
    else
        cout<<"y is greater";
}
Maksim Solovjov
  • 3,147
  • 18
  • 28
1

You cannot use:

friend void B :: max(A , B);

without the complete definition of B.

You'll need to rethink your strategy so that you can implement the functionality without using the friend declaration or move the definition of B ahead of the definition of A.

R Sahu
  • 204,454
  • 14
  • 159
  • 270