Thread: Declaring an instance of a class inside a class

  1. #1
    Registered Loser nickodonnell's Avatar
    Join Date
    Sep 2005
    Location
    United States
    Posts
    33

    Unhappy Declaring an instance of a class inside a class

    Here's the code:

    stats.h
    Code:
    class Stats
    {
          public:
                 Stats();
                 ~Stats();
                 
                 void power (int pow);
                 void vitality (int vit);
                 void magic (int mag);
                 void maxhealth (int maxh);
                 void health (int hea);
                 void maxmana (int maxm);
                 void mana (int man);
                 int displaypower();
                 int displayvitality();
                 int displaymagic();
                 int displaymaxhealth();
                 int displayhealth();
                 int displaymaxmana();
                 int displaymana();
          protected:
                    unsigned int strength;
                    unsigned int defense;
                    unsigned int intelligence;
                    unsigned int maxhp;
                    signed int hp;
                    unsigned int maxmp;
                    unsigned int mp;
    };
    
    class Hero :public Stats
    {
          public:
                 Hero();
                 ~Hero();
                 
                 void level_up();
                 void level_stats();
                 void char_class_stats();
                 //1-human 2-dwarf 3-elf
                 
                 //character
                 Hero character;
                 character.power(pow);
                 character.vitality(vit);
                 character.magic(mag);
                 character.maxhealth(maxh);
                 character.health(hea);
                 character.maxmana(maxm);
                 character.mana(man);
                 
                 //human
                 Hero human;
                 human.power(10);
                 human.vitality(10);
                 human.magic(10);
                 human.maxhealth(100);
                 human.health(100);
                 human.maxmana(10);
                 human.mana(10);
                 
                 //dwarf
                 Hero dwarf;
                 dwarf.power(15);
                 dwarf.vitality(15);
                 dwarf.magic(5);
                 dwarf.maxhealth(150);
                 dwarf.health(150);
                 dwarf.maxmana(5);
                 dwarf.mana(5);
                 
                 //elf
                 Hero elf;
                 elf.power(5);
                 elf.vitality(10);
                 elf.magic(15);
                 elf.maxhealth(50);
                 elf.health(50);
                 elf.maxmana(15);
                 elf.mana(15);
          protected:
                    unsigned int char_level;
                    unsigned int char_exp;
                    unsigned int char_class;
    };
    
    //stat function declarations start
    Stats::Stats()
    {}
    Stats::~Stats()
    {}
    Hero::Hero()
    {}
    Hero::~Hero()
    {}
    void Stats::power (int pow)
    {
         strength=pow;
    }
    void Stats::vitality (int vit)
    {
         defense=vit;
    }
    void Stats::magic (int mag)
    {
         intelligence=mag;
    }
    void Stats::maxhealth (int maxh)
    {
         maxhp=maxh;
    }
    void Stats::health (int hea)
    {
         hp=hea;
    }
    void Stats::maxmana (int maxm)
    {
         maxmp=maxm;
    }
    void Stats::mana (int man)
    {
         mp=man;
    }
    int Stats::displaypower()
    {
        return strength;
    }
    int Stats::displayvitality()
    {
        return defense;
    }
    int Stats::displaymagic()
    {
        return intelligence;
    }
    int Stats::displaymaxhealth()
    {
        return maxhp;
    }
    int Stats::displayhealth()
    {
        return hp;
    }
    int Stats::displaymaxmana()
    {
        return maxmp;
    }
    int Stats::displaymana()
    {
        return mp;
    }
    //stat function declarations end
    
    //character class start
    
    void Hero::char_class_stats()
    {
         switch (char_class)
         {
                case 1:
                     character.power(pow) = human.power(pow);
                     character.vitality(vit) = human.vitality(vit);
                     character.magic(mag) = human.magic(mag);
                     character.maxhealth(maxh) = human.maxhealth(maxh);
                     character.health(hea) = human.health(hea);
                     character.maxmana(maxm) = human.maxmana(maxm);
                     character.mana(man) = human.mana(man);
                     break;
                case 2:
                     character.power(pow) = dwarf.power(pow);
                     character.vitality(vit) = dwarf.vitality(vit);
                     character.magic(mag) = dwarf.magic(mag);
                     character.maxhealth(maxh) = dwarf.maxhealth(maxh);
                     character.health(hea) = dwarf.health(hea);
                     character.maxmana(maxm) = dwarf.maxmana(maxm);
                     character.mana(man) = dwarf.mana(man);
                     break;
                case 3:
                     character.power(pow) = elf.power(pow);
                     character.vitality(vit) = elf.vitality(vit);
                     character.magic(mag) = elf.magic(mag);
                     character.maxhealth(maxh) = elf.maxhealth(maxh);
                     character.health(hea) = elf.health(hea);
                     character.maxmana(maxm) = elf.maxmana(maxm);
                     character.mana(man) = elf.mana(man);
                     break;
         }
    }
    
    //character class end
    
    //levels start
    
    void Hero::level_stats()
    {
         unsigned int char_level=1;
         switch (char_level)
         {
                case 1:
                     character.power(pow) = character.power(pow);
                     character.vitality(vit) = character.vitality(vit);
                     character.magic(mag) = character.magic(mag);
                     character.maxhealth(maxh) = character.maxhealth(maxh);
                     character.health(hea) = character.health(hea);
                     character.maxmana(maxm) = character.maxmana(maxm);
                     character.mana(man) = character.mana(man);
                     break;
                case 2:
                     character.power(pow) = character.power(pow) + 2;
                     character.vitality(vit) = character.vitality(vit) + 2;
                     character.magic(mag) = character.magic(mag) + 2;
                     character.maxhealth(maxh) = character.maxhealth(maxh) + 20;
                     character.health(hea) = character.health(hea) + 20;
                     character.maxmana(maxm) = character.maxmana(maxm)+ 5;
                     character.mana(man) = character.mana(man) + 5;
                     break;
                case 3:
                     character.power(pow) = character.power(pow) + 4;
                     character.vitality(vit) = character.vitality(vit) + 4;
                     character.magic(mag) = character.magic(mag) + 4;
                     character.maxhealth(maxh) = character.maxhealth(maxh) + 40;
                     character.health(hea) = character.health(hea) + 40;
                     character.maxmana(maxm) = character.maxmana(maxm)+ 10;
                     character.mana(man) = character.mana(man) + 10;
                     break;
         }
    }
    
    void Hero::level_up()
    {
         if ((char_exp > 50) && (char_level==1))
         {
                       char_level++;
                       cout<<"\nYou are now level"<<char_level<<"!\n\n";
         }
         else if ((char_exp > 100) && (char_level==2))
         {
                       char_level++;
                       cout<<"\nYou are now level"<<char_level<<"!\n\n";
         }
    }
    
    //levels end
    main.cpp
    Code:
    #include <string>
    #include <iostream>
    #include "stats.h"
    
    using namespace std;
    
    int main()
    {
        cin.get();
    }
    It won't compile for the same reason about 50 times: ISO C++ forbids declaration of 'character' with no type.

    What I tried to do in the inherited class Hero was declare an instance of Hero as each race (or char_class). Why won't it work? Should I change it to, let's say, Stats character, character.power(int pow), etc...? What could I do to make this work? Help is extremely appreciated.
    Last edited by nickodonnell; 10-01-2005 at 09:03 PM.

  2. #2
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    You shouldnt (or cant) initialize things inside of the class declarations.

    Code:
    class Hero : public Stats
    {
          public:
                 Hero();
                 ~Hero();
    
                 void level_up();
                 void level_stats();
                 void char_class_stats();
    
          protected:
                 unsigned int char_level;
                 unsigned int char_exp;
                 unsigned int char_class;
          private:
                 Hero* character;
                 Hero* human;
                 Hero* dwarf;
                 Hero* elf;
    };
    
    Hero::Hero()
    {
                 //1-human 2-dwarf 3-elf
    
                 //character
                 character = new Hero;
                 character->power(pow);
                 character->vitality(vit);
                 character->magic(mag);
                 character->maxhealth(maxh);
                 character->health(hea);
                 character->maxmana(maxm);
                 character->mana(man);
                 
                 //human
                 human = new Hero;
                 human->power(10);
                 human->vitality(10);
                 human->magic(10);
                 human->maxhealth(100);
                 human->health(100);
                 human->maxmana(10);
                 human->mana(10);
                 
                 //dwarf
                 dwarf = new Hero;
                 dwarf->power(15);
                 dwarf->vitality(15);
                 dwarf->magic(5);
                 dwarf->maxhealth(150);
                 dwarf->health(150);
                 dwarf->maxmana(5);
                 dwarf->mana(5);
                 
                 //elf
                 elf = new Hero;
                 elf->power(5);
                 elf->vitality(10);
                 elf->magic(15);
                 elf->maxhealth(50);
                 elf->health(50);
                 elf->maxmana(15);
                 elf->mana(15);
    }
    
    void Hero::char_class_stats()
    {
         switch (char_class)
         {
                case 1:
                     character->power(pow) = human->power(pow);
                     character->vitality(vit) = human->vitality(vit);
                     character->magic(mag) = human->magic(mag);
                     character->maxhealth(maxh) = human->maxhealth(maxh);
                     character->health(hea) = human->health(hea);
                     character->maxmana(maxm) = human->maxmana(maxm);
                     character->mana(man) = human->mana(man);
                     break;
                case 2:
                     character->power(pow) = dwarf->power(pow);
                     character->vitality(vit) = dwarf->vitality(vit);
                     character->magic(mag) = dwarf->magic(mag);
                     character->maxhealth(maxh) = dwarf->maxhealth(maxh);
                     character->health(hea) = dwarf->health(hea);
                     character->maxmana(maxm) = dwarf->maxmana(maxm);
                     character->mana(man) = dwarf->mana(man);
                     break;
                case 3:
                     character->power(pow) = elf->power(pow);
                     character->vitality(vit) = elf->vitality(vit);
                     character->magic(mag) = elf->magic(mag);
                     character->maxhealth(maxh) = elf->maxhealth(maxh);
                     character->health(hea) = elf->health(hea);
                     character->maxmana(maxm) = elf->maxmana(maxm);
                     character->mana(man) = elf->mana(man);
                     break;
         }
    }

    Might want to consider making the Stats constructor take values so setting up the stats would be a lot easier. You would have to overwrite the constructor in Hero so it worked though.

    I dont think you can do this either:
    character->power(pow) = human->power(pow);

    Unless you overloaded the Hero class somehow to take assignment operations. The correct way would be:

    character->power(human->displaypower());
    Last edited by Dae; 10-01-2005 at 09:16 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    Registered Loser nickodonnell's Avatar
    Join Date
    Sep 2005
    Location
    United States
    Posts
    33
    Whoa... it's late and my brain hurts. Thanks, though. I'll try to fix it.

  4. #4
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    The member variables of the same type as the class should be either pointers or references as well.

  5. #5
    Registered Loser nickodonnell's Avatar
    Join Date
    Sep 2005
    Location
    United States
    Posts
    33
    Alrighty, I'll get the rest posted when I get it done. Thanks for the help so far.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Create instance on class programatically
    By deviousdexter in forum C# Programming
    Replies: 6
    Last Post: 12-10-2008, 06:02 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. problem with const int inside class (c2258)
    By talz13 in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2003, 07:34 PM
  5. pointer to an instance of a different class
    By xlix in forum C++ Programming
    Replies: 4
    Last Post: 07-24-2003, 04:19 PM