I am trying to make an Array which will contain pointer to a class. The size of the array will be given from keyboard , so i tried to create the array with the following way :
class creature
{
protected:
string crt_name ;
int L ;
public:
creature( int Life = -1 , string Name = "")
: L(Life) , crt_name(Name){}
string get_crt_name ( void )
{
return crt_name ;
}
};
class creature_society
{
private:
creature* *A ;
int noc ;
public:
creature_society( int , int ) ;
~creature_society() ;
creature** get_A ( void ){return A ;}
};
The constructor of creature society will fill the array with creatures made randomly
creature_society::creature_society( int life , int number_of_creatures )
{
noc = number_of_creatures ;
A = new creature*[noc] ;
creature* temporary ;
for( int i = 0 ; i<= number_of_creatures -1 ; i++)
{
if ( rand()%100 <= 50)
{
temporary = new good_creature( life , get_unique_name( 3 ) );
A[i] = temporary ;
}
else
{
temporary = new bad_creature( life , get_unique_name( 3 ) );
A[i] = temporary ;
}
}
}
Then I try to print the array
cout << endl << "Printing Society:" << endl ;
for ( j = 0 ; j <= N -1 ; j++)
{
temp = (*( cs1.get_A() + j ))->get_crt_name() ;
}
The problem is that when i am getting segmentation faults when running on linux , while working fine on Dev C++ ( most of the time )! Any mistakes you pointed out ?