Public Group

# Class A uses Class B, and B uses A (in separate Files) problem

This topic is 3959 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi, im having some problem wih my classes, class A has a pointer type B. class B has an object to of type A,
//FILE A.h
Class A{
public:
B* pb;
};

//FILE B.h
Class B{
public:
A objecta;
};
they are in different files, i've tried adding class B; before defining class A but i didnt work, because they are in separate files.

##### Share on other sites
declare the class at the start eg:
class A;class B;Class A{	public:		B* pb;};//FILE B.hClass B{	public:		A objecta;};

##### Share on other sites
// classA.hclass B;class A {  B * b;};// classB.h#include "classA.h"   // you seem to be missing thisclass B {  A a;};

##### Share on other sites
If this situation occurs, it usually marks the time to re-examine your class structure. Ask yourself why some functionality needs to be in B when A needs to use it? Why not put it in A anyways?

Happy coding!

##### Share on other sites
now is working!

the problem was the same you said, so i fixed it and now i dondt have undefined classes.

thanks!

##### Share on other sites
Quote:
 Original post by LordShadeIf this situation occurs, it usually marks the time to re-examine your class structure.

There are plenty of good reasons, not the least of which is containment with back-references. That is, some class A which has a list of B and each B only has one A, so you should be able to also navigate from the B back to A.

##### Share on other sites
Quote:
 Original post by LordShadeIf this situation occurs, it usually marks the time to re-examine your class structure. Ask yourself why some functionality needs to be in B when A needs to use it? Why not put it in A anyways?Happy coding!

yes, you are right about that im re-examining the need of the object A atribute from class B

in my case one class is CProjectile (this would be class A)
that has a pointer to the CWeapon(this would be class B) that belongs to, so when doing the motion, collision and physics algorithms will be easier to deal cause i can grab all existing projectiles and recalculate them in relation to the weapon it was fired from.

up to this point, most of you should agree with the logic.

now comes the optional part, which i still dont know if i would need it for future:
until now CWeapon class had an array of objects CProjectile, a gun has projectiles so at first one should think that a weapon should have that array.
(update: like as SnprBoB86 said, to navigate from a to b and from b to a.)
but after thinking twice maybe is better to just not have any array and have a global one, i would loose B to A but still have A to B. i will see whats better over these days.
thanks

##### Share on other sites
I get what you're using it for. Ask yourself, why should a projectile need to know what it is fired from? In the real world, a 9mm fired from any pistol or submachine gun just knows that when it's primer is triggered, it ignites the powder and launches the round at some arbitrary velocity. The gun itself determines it's vector and spin.

So really, the round shouldn't know anything about the weapon it is fired from. The weapon should just set the vector and modify speed if necessary.

Pre-caching projectile classes will speed up your performance as you mentioned.

I'm not saying that it is wrong to have classes linking to each other as mentioned, all I'm saying is that more often than not, there is a better solution.

Happy coding.

##### Share on other sites
Quote:
 Original post by LordShadeI get what you're using it for. Ask yourself, why should a projectile need to know what it is fired from? In the real world, a 9mm fired from any pistol or submachine gun just knows that when it's primer is triggered, it ignites the powder and launches the round at some arbitrary velocity. The gun itself determines it's vector and spin.So really, the round shouldn't know anything about the weapon it is fired from. The weapon should just set the vector and modify speed if necessary.Pre-caching projectile classes will speed up your performance as you mentioned.I'm not saying that it is wrong to have classes linking to each other as mentioned, all I'm saying is that more often than not, there is a better solution.Happy coding.

yes, but you know that linking the projectile to the weapon, you can make wonderful creations like boomerang style bullets, rockets that orbit around you changing dinamically, energy rails that connect you and the target at every moment, well lots of intelligent projectiles and of course the classical bullets that you mention dont get affected by this structure.

##### Share on other sites
hehe, nice.

Give the projectile a pointer to the vertex of the weapon instead since your more interested in the data than the functionality.

Happy coding!

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5
khawk
11

• 9
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633679
• Total Posts
3013299
×