E
Eternally
Ok, this may sound confusing....but it's really simple. If you're confused,
just look at my example code and it'll make sense.
Here's my situation. I have 2 classes....A and B.
Class A has a member variable of type B.
Class B has a member function which does calculations which are dependent
upon values of members of the Class A that owns it.
When in that function of class B, I don't know how I can gain access to it's
parent Class A's member variables, so my solution to the problem is to take
as a parameter to that function, a class A datatype. So, it's like this:
class A
{
public:
int x;
B myB;
:
:
};
class B
{
public:
int y;
void theFunction(A parentA);
:
:
};
void B::theFunction(A parentA){
y = parentA.x * 10;
}
I would call it like this:
A myA;
myA.myB(myA);
Now, the problem is, both A.h and B.h have to include eachother. When they
do, I get the following compilation errors:
A.h(10): error C2143: syntax error : missing ';' before 'B'
A.h(10): error C2501: 'A::B' : missing storage-class or type specifiers
Line 10 is the line that I declare B myB is A.h.
If I comment out theFunction in B and I comment out the include of A.h,
it'll compile no problem. But that obviously isn't the solution as B can't
use A.
So, I need one of 2 solutions. Either (and most preferably) I somehow gain
access to the parent object A's member variables, without having to pass A
as a parameter to B's function....or, I keep it like it is, but somehow get
it to compile.
Can anyone inform me how either of the above can be accomplished?
Oh...and I know I could just pass myA.x, but don't want to do that as the
real function is called often and is actually dependent upon many member
variables of A.
Thanks a lot for any help!
just look at my example code and it'll make sense.
Here's my situation. I have 2 classes....A and B.
Class A has a member variable of type B.
Class B has a member function which does calculations which are dependent
upon values of members of the Class A that owns it.
When in that function of class B, I don't know how I can gain access to it's
parent Class A's member variables, so my solution to the problem is to take
as a parameter to that function, a class A datatype. So, it's like this:
class A
{
public:
int x;
B myB;
:
:
};
class B
{
public:
int y;
void theFunction(A parentA);
:
:
};
void B::theFunction(A parentA){
y = parentA.x * 10;
}
I would call it like this:
A myA;
myA.myB(myA);
Now, the problem is, both A.h and B.h have to include eachother. When they
do, I get the following compilation errors:
A.h(10): error C2143: syntax error : missing ';' before 'B'
A.h(10): error C2501: 'A::B' : missing storage-class or type specifiers
Line 10 is the line that I declare B myB is A.h.
If I comment out theFunction in B and I comment out the include of A.h,
it'll compile no problem. But that obviously isn't the solution as B can't
use A.
So, I need one of 2 solutions. Either (and most preferably) I somehow gain
access to the parent object A's member variables, without having to pass A
as a parameter to B's function....or, I keep it like it is, but somehow get
it to compile.
Can anyone inform me how either of the above can be accomplished?
Oh...and I know I could just pass myA.x, but don't want to do that as the
real function is called often and is actually dependent upon many member
variables of A.
Thanks a lot for any help!