Search code examples
c++friend-function

forward declaration is not accepted


class B;
class A
{
    int divident,divisor;
    friend int B::test();
public:
     A(int i,int j):divident(i),divisor(j){}
};

class B
{
public:
    int test();
};

int B::test(){}
int main(){return 1;}

It throws following error in Qt Creator with Mingwin.

enter image description here

Why is it throwing an error for forward declaration of class B? May be I am making a silly mistake, but I am not able to find it.


Solution

  • When the compiler is parsing statement

    friend int B::test();
    

    it does not know whether class B has member function test. In this point the compiler needs the definition of class B that to determine whether this statement is correct.

    Place the definition of class B before the definition of class A.