Using a class as a type doesnt work? how then?

Started by
10 comments, last by xyuri 19 years, 4 months ago
I have two classes, which is all fine and dandy as you would expect. One is a generic PLAYER class and another is a generic NPC class (I was really just messing around to help someone else out).... The two classes are as follows:
class PLAYER
{
public:

    double health;
    double offensive;
    double defensive;

    PLAYER ()
    {
    health = 100;
    offensive = 20;
    defensive = 20;
    }
    
    int TakeDamage (int Damage)
    {
        //int diff = (defensive - opponent.offensive);
        health -= Damage;
        if (health <= 0) {return 0;} else {return 1;}
    }
};

class NPC
{
public:

    bool isenemy;
    bool willattack;
    double health;
    double offensive;
    double defensive;
    
    NPC ()
    {
    isenemy = true;
    willattack = false;
    health = 100;
    offensive = 20;
    defensive = 50;
    }

    int TakeDamage (int Damage)
    {
        health -= Damage;
        if (health <= 0) {return 0;} else {return 1;}
    }

};



As you can see there is a TakeDamage function for these that allow you to specify how much you want to danage the character. But I want to be able to give an NPC as the argument for TakeDamage (and PLAYER for the NPS's TakeDamage function). If I just go something like this:
int TakeDamage (NPC opponent)
{
    //int diff = (defensive - opponent.offensive);
    //etc....
}
it gives me the following error: 'opponent' has incomplete type Is there something I have to do to the class to allow it to be used like this (as a type)?
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend
Advertisement
well that last bit of code doesn't even make sense.
you take NPC as an argument but use it nowhere in your code.

there are a few ways to get your function to act the way you want it too, but i suggest you make the TakeDamage exactly the way you want it so that we can help you with a better solution.

Beginner in Game Development?  Read here. And read here.

 

You need to forward declare NPC so that class PLAYER knows it exists. Add the following line before your declaration of class PLAYER:

class NPC;

This tells class PLAYER that NPC exists but will be declared later. You dont need to do the reverse for the declaration of class NPC as class PLAYER is known at that point.
you could try deriving the classes i don't know if thats the right way to do it.
Bring more Pain
try using a pointer to an NPC. And, before the player class definition, have a forward class declaration for the NPC class, like this:
class NPC;
Alpha_ProgDes: I dont need to put anything in that function, all you need to know is that I need to use an NPC class in it :p

Anonymous Poster: I did declare it before the PLAYER class, if I didnt I get am "'NPC' was not declared in this scope" error.

owiley: I have no idea how to do that. But if someone can confirm that this is the was to get around this then I'll find out how to do that :)

Thanks for the input guys :)
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend
Quote:Original post by Roboguy
try using a pointer to an NPC. And, before the player class definition, have a forward class declaration for the NPC class, like this:
class NPC;
WooHoo !!!!!!! *cracks open champaign bottle*

I was planning on making it a pointer, but I thought it should work either way. Hey, you learn something new every day 'eh :-)

Thank you very much guys :-)
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend
Obviously if I'm going to do the same for both classes I need to foreward declare one of them, but when I do that it gives me errors again ....

I have the following code now:
class NPC;class PLAYER{public:    double health;    double offensive;    double defensive;    PLAYER ()    {    health = 100;    offensive = 20;    defensive = 20;    }    int TakeDamage (NPC* opponent)    {        int diff = ((int)defensive - (int)opponent->offensive);        int damage = 100 - (50 + diff);        health -= damage;        if (health <= 0) {return 0;} else {return 1;}    }};class NPC{public:    bool isenemy;    bool willattack;    double health;    double offensive;    double defensive;        NPC ()    {    isenemy = true;    willattack = false;    health = 100;    offensive = 20;    defensive = 50;    }    int TakeDamage (int Damage)    {        health -= Damage;        if (health <= 0) {return 0;} else {return 1;}    }};
and just because I swapped the position of the classes it gives me the following error(s):
invalid use of an undefined type 'struct NPC'
forward declaration of 'struct NPC'


This is odd?
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend
I don't think you can do it like this.

If you are just declaring use of a class-pointer in a file, then you can forward declare the class name. But you're not - you're defining the function and using the forward-declared class prior to a full class definition, which I don't believe you can do.

Try this:

class NPC;class PLAYER{public:    double health;    double offensive;    double defensive;    PLAYER ()    {    health = 100;    offensive = 20;    defensive = 20;    }    int TakeDamage (NPC* opponent);};class NPC{public:    bool isenemy;    bool willattack;    double health;    double offensive;    double defensive;        NPC ()    {    isenemy = true;    willattack = false;    health = 100;    offensive = 20;    defensive = 50;    }    int TakeDamage (int Damage)    {        health -= Damage;        if (health <= 0) {return 0;} else {return 1;}    }};int PLAYER::TakeDamage(NPC* opponent)    {        int diff = ((int)defensive - (int)opponent->offensive);        int damage = 100 - (50 + diff);        health -= damage;        if (health <= 0) {return 0;} else {return 1;}    }


HTH,
Jim.


Edit : you might also want to define your constructors like this, I believe there are several advantages:

Player::Player() : health(100), offensive(20), (etc.)
{}
You really need to do it like that !! OMG.

Oh well, what needs to be done needs to be done :-)

I am aware of prototyping classes and functions but dont always use them for everything.

Thank you very much dude.
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend

This topic is closed to new replies.

Advertisement