1

I am a novice to C++ and having the following issue:

I have a parent class, called Creature:

class Creature
{
public:
    bool isActive;
    std::string name;
    int attackNumOfDice, attackNumOfSides, 
                defenseNumOfDice, defenseNumOfSides;
    int armor, strength; 

    Creature();
    //two virtual functions determine the damage points
    virtual int attack(); 
    virtual int defend();
    void halveAttackDice(); 
    void setStrength(int toSet);
    void setStatus(bool activity);

};

and 5 child classes like so:

.h file:

class Child : public Creature
{
int attack();
int defend();
}

implementation file:

    int Child::isActive = true;
    std::string Child::name = "";
    int Child::attackNumOfDice = 2;
    ...

    int Child::attack()
{
...
}
    intChild::defend()
{
...}

however, when I try to compile like this I get the same error for all 5 child classes:

child.cpp:6: error: ‘bool Child::isActive’ is not a static member of ‘class Child’
child.cpp:7: error: ‘std::string Child::name’ is not a static member of ‘class Child’
child.cpp:8: error: ‘int Child::attackNumOfDice’ is not a static member of ‘class Child’
...

I don't understand why is saying not a static member when I never defined one?

1 Answers1

1

You are trying to access class members without a object context. The error relies on the fact that you are trying to initialize the class attributes as they were static.

This is wrong:

int Child::isActive = true;
std::string Child::name = "";
int Child::attackNumOfDice = 2;

It is wrong because when we talk about non-static attributes, they have to be related to an object. The way you are giving default values to the attributes, you are not relating them with any object.

If you want to give default values for class attributes, do so inside a constructor, more specifically using initializer list (take a look in here)

Child::Child() : Creature(){
    ...
}

...

Creature::Creature() : isActive(false), name(""){
    ...
}

Whenever the constructor is called (or any non static class method), an implicit object reference (also known as pointer this) is passed to it. This way, the attributes access always occur using this object context.

Community
  • 1
  • 1
igor.araujo
  • 1,007
  • 1
  • 7
  • 15
  • thanks...is there a way to do it using just the constructor in Parent class? the prompt says there should only be one constructor – Robert Daniel Ottolia Feb 12 '15 at 02:18
  • yes... you can call parent constructor from the subclass class constructor using this sintax: Child::Child() : Creature(){ }. You just have to leave your subclass constructor empty, and it will run the code that exists on the parent constructor. – igor.araujo Feb 12 '15 at 02:24
  • 1
    If you are trying to initialize variables in the constructor, then use the constructor initializer list, intead of assignment statements in the constructor body. – M.M Feb 12 '15 at 03:26
  • @MattMcNabb, you are right. That would be the correct way. I'm going to edit my answer. – igor.araujo Feb 12 '15 at 03:29