• Advertisement
Sign in to follow this  

help on my code

This topic is 4280 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

could someone help me on my code my first header is
class human{
int two
int three
int four
void attack(int &amount, enemy &name);
void magic(int &type, enemy &name);
};

my second header is
class enemy{
int two
int three
int four
void attack(int &amount, human &name);
void magic(int &type, human &name);
};

the functions are in there own source file the problem is when i try to run it it says class enemy not declared in the first and class human in the second and if i declare them by #include "human.h" and #include "orc.h" the compilation doesn't end any help?

Share this post


Link to post
Share on other sites
Advertisement
The problem is that the compiler has no idea what a 'human' is when it's reading the enemy header, and no idea what an enemy is when it's reading the human header.

The obvious solution would seem to be to include enemy.h in human.h and human.h in enemy.h, but that will just cause a recursive inclusion which won't work either. What you need is a forward definition - basically just a simple line of code that tells the compiler not to worry too much about what an enemy is, you'll explain it later.

e.g

class enemy; // this is a forward definition

class human{
int two
int three
int four
void attack(int &amount, enemy &name);
void magic(int &type, enemy &name);
};





class human; // this is also a forward definition

class enemy{
int two
int three
int four
void attack(int &amount, human &name);
void magic(int &type, human &name);
};





Since the forward definition contains no actual information about what it's defining, your header can contain only pointers and references to the forward defined class, and you can't try and use any of it's functions or data. For example, this wouldn't work in your header file:



class human; // this is also a forward definition

class enemy{
int two
int three
int four
void attack(int &amount, human name)
{
name.attack(10,*this);
}
void magic(int &type, human &name);
};






This fails for two reasons: first, name is no longer a reference in the attack function. since the compiler has no idea how big a 'human' is with only the forward definition to go on, it can't work out the function's profile properly. Second, we're calling the human's 'attack' function - but we haven't told the compiler anything about the human's functions. We can call functions in the cpp once we've got the full declaration, we just can't do it in the header file before then.

Also, wrong forum [grin]. The game design forum is for gameplay design and balance etc rather than coding.

Share this post


Link to post
Share on other sites
Just a small note: both classes have several similar variabeles. You may want to consider creating a superclass and deriving both the Human and Enemy class from it, extending them with their own specific functions and variabeles. I'm sure they share some behaviour like moving and the like. :)

It's also usefull to use virtual functions, for example an Update() function. You could create a list of Characters (the superclass) and store Humans and Enemy objects in them, and call the Update function for all of them because it's a Character member function. Since it's virtual, derived classes can define their own versions of this Update() function. Perhaps it doesn't really fit in your design so far, but it might come in handy at some time.

Share this post


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

  • Advertisement