Jump to content
  • Advertisement
Sign in to follow this  
xyuri

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

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

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)?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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 :-)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.)
{}

Share this post


Link to post
Share on other sites
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.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!