0

I am trying to make a Array of arms in the Creature class and all the creatures to share the same array of objects(arms). So I tried to do that ...but I don't know how to repair this problem ... please try to explain me at a beginner level, also if you can please provide me some links to read about using "static" correctly !

#include<iostream>
namespace
{
    int x = 5;
}

class Arms
{
    public:
    int arms = 45;
};

class Creature 
{
public : int health;
        int mana;
        int dmg;

         Arms *b[188]; 

        Creature(int);

};
Creature::Creature(int z )
{



    for(int i = 0 ;i< z; i++)
    {
        b[i] = new Arms;  //<---this is my problem
        b[i]->arms = z;  // <-- this is my problem
    }
}


int main()
{

    Creature c1(12);

    return 0;
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
user2979239
  • 9
  • 1
  • 2
  • If you want it static why didn't you declare it static? Do you want a static variable to be reinitialized everytime a new Creature is created? Why are you setting it's length to an arbitrary 188 instead of what is passed in the constructor? – Kevin DiTraglia Nov 11 '13 at 13:18
  • As for reading material, there's a [list of good books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Angew is no longer proud of SO Nov 11 '13 at 13:24

2 Answers2

0

First of all you should not use the C-like arrays. Use an STL container, such as std::array (fixed size, compiler-time know, sequence) or std::vector (variable-size sequence). That will make your like easier.

Second, if you want to share the same instance of the arms array with all the creatures you can make it static, indeed.

// in the header
class Creature 
{
public : 
  int health;
  int mana;
  int dmg;

  static std::vector<Arms> arms;

  Creature(int);

};

// in the .cpp file
std::vector<Arms> Creature::arms;
Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
0

I'm not sure what you are trying to achieve but I can see a few problems with your code:

class Arms
{
    public:
    int arms = 45; // this is pointless since only const members can be initialized within a class; in case you want this to be the default value you should write your own default constructor.
};

As far as I can tell the rest of your code compiles and it'll initialize z elements of array b to the value of z. but if you declare a new Creatures object, the new object will have it's own copy of the b array. To solve this you declare the array static. And since you are doing that you cannot have an array of pointers anymore since pointers can contain memory addresses and you need to point to the same location in memory: static Arms b[188];

the full code looks now like this:

class Creature 
{
public : int health;
    int mana;
    int dmg;

    static Arms b[188]; // this is only a declaration

    Creature(int);

};

Arms Creature::b[188];// you have to define this outside the class or you'll get a linker error.

Creature::Creature(int z )
{
    for(int i = 0 ;i< z; i++)
    {   
        b[i].arms = z;      
    }
}

Now if you have one or more Creature objects they'll all share the same array and see the same values for all elements.

Pandrei
  • 4,843
  • 3
  • 27
  • 44