Jump to content
  • Advertisement
Sign in to follow this  
Chire

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
// classA.h

class B;

class A {
B * b;
};



// classB.h
#include "classA.h" // you seem to be missing this

class B {
A a;
};

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by LordShade
If 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 this post


Link to post
Share on other sites
Quote:
Original post by LordShade
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!



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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by LordShade
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.


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 this post


Link to post
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!