declarations for class dependencies

J

Jess

Hello,

I'd like to declare two classes to be friend classes, I did the
following:

#include<iostream>

using namespace std;

class A{
friend class B;
public:
int x;
B b;
A():x(10),y(20){}
private:
int y;
};

class B{
friend class A;
public:
int u;
B():u(30),v(40){}
A a;
private:
int v;
};

int main(){
B b;
return 0;
}

However, the compiler error said "'B' does not name a type". I think
this is because I haven't declared
B when I introduced it into A. But if I declare class B first, then
the same problem will happen to the declaration of B. What should I
do?

Thanks,
Jess
 
P

Pradeep

Hello,

I'd like to declare two classes to be friend classes, I did the
following:

#include<iostream>

using namespace std;

class A{
friend class B;
public:
int x;
B b;
A():x(10),y(20){}
private:
int y;

};

class B{
friend class A;
public:
int u;
B():u(30),v(40){}
A a;
private:
int v;

};

int main(){
B b;
return 0;

}

However, the compiler error said "'B' does not name a type". I think
this is because I haven't declared
B when I introduced it into A. But if I declare class B first, then
the same problem will happen to the declaration of B. What should I
do?

Thanks,
Jess

no with B,this problem will not come because u have already define the
class A.
so for class B, A is not a unkown symbol.
is it what u want to ask.....?
 
P

Paolo Maldini

i'm afraid you couldn't do such declaration.
try this way:

class B;

class A{
friend class B;
public:
int x;
B *pb;
A():x(10),y(20){}
private:
int y;
};

class B{
friend class A;
public:
int u;
B():u(30),v(40){}
A a;
private:
int v;
};

BR,
-Paolo
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hello,

I'd like to declare two classes to be friend classes, I did the
following:

#include<iostream>

using namespace std;
>
class A{
friend class B;
public:
int x;
B b;
A():x(10),y(20){}
private:
int y;
};

class B{
friend class A;
public:
int u;
B():u(30),v(40){}
A a;
private:
int v;
};

int main(){
B b;
return 0;
}

However, the compiler error said "'B' does not name a type". I think
this is because I haven't declared
B when I introduced it into A. But if I declare class B first, then
the same problem will happen to the declaration of B. What should I
do?

You have two problems where, the first is the one you see, to solve that
one simply add a forward declaration of B above the declaration of A. A
forward declaration looks like this:

class B;

This will tell the compiler that there is a class named B so it won't
stop in your friend declaration.

The second problem is that both A and B have a member which is of the
other type, this can never work since it requires the compiler to know
the size of class B when constructing class A and the size of class A
when constructing class B.

To solve this you need to make either the A member of B a pointer or
reference of do the same to the B member of A, whichever you choose
depends on how you are going to use them.

So a working solution might look like this:

// Forward declaration
class B;

class A{
friend class B;
public:
int x;
B* b; // Notice the *
A():x(10),y(20){}
private:
int y;
};

class B{
friend class A;
public:
int u;
B():u(30),v(40){}
A a;
private:
int v;
};

int main(){
B b;
return 0;
}
 
J

Jess

no with B,this problem will not come because u have already define the
class A.
so for class B, A is not a unkown symbol.
is it what u want to ask.....?

I meant if I swap the declarations of class B and A, so that B comes
first, then A is defined afterwards. In that case, A isn't defined
when B is defined.

Thanks,
Jess
 
J

Jess

To solve this you need to make either the A member of B a pointer or
reference of do the same to the B member of A, whichever you choose
depends on how you are going to use them.

Thanks a lot. I also tried to use reference, by having:

class A{
friend class B;
public:
int x;
B& b;
A():x(10),y(20){}
private:
int y;
};

The compiler error was: "uninitialized reference member 'A::b'". If I
have an ordinary member that is of class type (rather than reference
type), then compiler wouldn't complain. Why does it complain now? I
modified the constructor to:

A(B& z):x(10),y(20){b = z;}

But I also got many error messages. How should I init a member of
reference type? Additionally, how do I choose between a pointer-typed
member and a reference-typed member?

Many thanks,
Jess
 
P

Pradeep

I meant if I swap the declarations of class B and A, so that B comes
first, then A is defined afterwards. In that case, A isn't defined
when B is defined.

Thanks,
Jess

yes at that time it will give error that A is not defined.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Thanks a lot. I also tried to use reference, by having:

class A{
friend class B;
public:
int x;
B& b;
A():x(10),y(20){}
private:
int y;
};

The compiler error was: "uninitialized reference member 'A::b'". If I
have an ordinary member that is of class type (rather than reference
type), then compiler wouldn't complain. Why does it complain now? I
modified the constructor to:

A(B& z):x(10),y(20){b = z;}

But I also got many error messages. How should I init a member of
reference type?

If you use a reference it must be initialized in the initializer-list:

A(B& z):x(10),y(20), b(z) {}
Additionally, how do I choose between a pointer-typed member and a
reference-typed member?

If you use a reference you must initialize it in the initializer list,
which means that you can have a default constructor, you must always
take an B object as parameter. Nor can you ever change the B object
referred to.

Using a pointer you can have a default constructor since you can
initialize the pointer to 0 (or create a new one), and you can later
change the object pointed to.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top