sirsk8aton Posted May 2, 2012 Author Share Posted May 2, 2012 I have a simple(at least i think) question for anyone with some decent C++ knowledge.My problem:Using my own classes/objects in other classes/objects correctly.The way I have it set up right now is very unreliable and probably considered bad code. What I have is a header file named "Includes.h" and I simply put all includes I use in my project in that file. I can use some of my own classes in other classes, as long as they're defined before the other class is.Example:Includes.h:```#include "Player.h"#include "Monster.h"```Player.h:```class Player{ void attackMonster(Monster tempMonster) //Error, type Monster not defined};```Monster.h```class Monster{ void attackPlayer(Player tempPlayer) //Works because Player.h is defined before Monster.h};```I've tried some methods to get around this but they still don't work properly. I would like to know the most efficient way of doing this without errors. It'd be much appreciated. Thanks! Link to comment Share on other sites More sharing options...
unknown Posted May 2, 2012 Share Posted May 2, 2012 Include monster from player.Also, make sure to have stuff like this in your header files:```#ifndef _MONSTER_H#define _MONSTER_H.. class stuff ..#endif```I'm not sure how to fix your circular problem.. According to Wikipedia you have to use a [forward declaration](http://en.wikipedia.org/wiki/Forward_declaration) trick which seems hacky. **Classes shouldn't depend on eachother that way** which is a bad answer, but true.http://en.wikipedia.org/wiki/Circular_dependencyHere's the exact code from wikipedia for creating a forward declaration**File a.h:**```#ifndef A_H#define A_Hclass B; //forward declarationclass A {public: B* b;};#endif //A_H```**File b.h:**```#ifndef B_H#define B_Hclass A; //forward declarationclass B {public: A* a;};#endif //B_H```**File main.cpp:**```#include "a.h"#include "b.h"int main() { A a; B b; a.b = &b; b.a = &a;}``` Link to comment Share on other sites More sharing options...
Godlord Posted May 3, 2012 Share Posted May 3, 2012 To quote my code convention for C programming:> Header files must contain safety guards using the preprocessor. Within projects> the name used must be of the form: __NAMESPACE_FILENAME_HEADER__, where> NAMESPACE and FILENAME are to be replaced by the namespace used for the project,> and the name of the file respectively. Both must be in uppercase. When a> namespace is not used, the name must be of the form: __FILENAME_HEADER__.> > You may only include header files when necessary. It is encouraged to use> forward declarations instead, especially within header files. To resolve cyclic> dependencies one may use typedef safety guards, where the name used is of the> form: __TYPENAME_TYPE__, where TYPENAME is to be replaced by the type name. One> may consider using a full declaration when absolutely necessary.@unknown: there are cases where cyclic dependencies are impossible to prevent without changing the entire design of the project.Yours faithfully S.J.R. van Schaik. Link to comment Share on other sites More sharing options...
sirsk8aton Posted May 4, 2012 Author Share Posted May 4, 2012 So, basically in my example, would I have to do something like this?```#ifndef _PLAYER_HEADER_#define _PLAYER_HEADER_class Monster;class Player{ void attackMonster(Monster tempMonster)};#endif```Would I also need to include the monster header here or in the Player.cpp file? Link to comment Share on other sites More sharing options...
unknown Posted May 4, 2012 Share Posted May 4, 2012 Yeah that looks right. Since you will be doing stuff with Monster in Player you will have to include it from the Player.cpp file Link to comment Share on other sites More sharing options...
Minikloon Posted May 4, 2012 Share Posted May 4, 2012 Why would you want to abstract all the damage values?You could use a simple hit(int damage) function instead.Also, you may read up on https://www.google.ca/search?q=c%2B%2B+inheritance.Monster and Player obviously have common properties, like damage values and hit points. If you put those properties into a Creature class in make Monster and Player inherit from it, you won't have the looping inclusion problem. Link to comment Share on other sites More sharing options...
Admiral Refuge Posted May 4, 2012 Share Posted May 4, 2012 On a side note, if you're using C++, you might want to set your headers as .hpp instead of .h; the latter is designed for C. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now