Declaring member function of another class as friend before it isdefined

C

Cotangent Alpha

When class A is declared before class B is defined, I am unable to
make B::baz() a friend function of class A. I get this error: member
'void B::baz()' declared as friend before type 'B' defined. Is it
possible to resolve this?

Here is the code:

class B;

class A
{
public:
void foo() {
cout << "A::foo()" << endl;
}

private:
// This function should be accessible to B only and nothing else.
void bar() {
cout << "A::bar()" << endl;
}
friend void B::baz();
};


class B
{
public:
void baz() {
A a;
a.bar();
}
};

int main()
{
B b = b;
b.baz();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top