0

I am trying to create a vector of objects but i have some issues. I can't push_back over 19 objects to my vector because it shows up an error message of bad_alloc.

I try to resize my vector with resize() or reserve() but still nothing.

For resize(), I read that you need to provide 2 arguments to resize a vector.But still nothing. When I try to use it without push_back it shows error: expected primary-expression before ')' token.

#define N 10 //ari8mos seirwn tou xarth
#define M 10 //ari8mos sthlwn tou xarth
#define TREAS 100//posothta 8usaurou
#define PORTS 100//ari8mos limaniwn
extern void ships(map (&myArray)[N][M], vector<ship> &myShips);

void ships(map (&myArray)[N][M], vector<ship> &myShips)
{
  int i,j,y;

  srand ( time(NULL) );
  //myShips.reserve(21);
  //myShips.resize(20,ship);
  cout << myShips.capacity() << endl;
  int x=0;
  for( i = 0; i <19 ; i++){
      myShips.push_back(pirate(rand() % N,rand() % M,100,100,100,1,'@',myArray,myShips));
  }
  for( i=0;i<myShips.size();i++ ){
      cout << myShips[i].get_symbol() << " ";
  }

}

here is the rest of code to help you understand:

  class ship
{
   protected:
    int i,j,x2,y2;
    //vector<vector<map> > myArray;
    //ship (&myShips)[N][M];
    int x;
    int y;
    map (myArray)[N][M];
    vector<ship> myShips;
    int max_resistance;
    int current_resistance;
    int speed;
    int reserve_treasure;
    char symbol;

   public:
    ship(int x_, int y_, int max_res, int cur_res, int res_treas, int sp, char sy, map (&myArr)[N]     [M], vector<ship> &Ship)
        :x(x_)
        ,y(y_)
        ,max_resistance(max_res)
        ,current_resistance(cur_res)
        ,reserve_treasure(res_treas)
        ,speed(sp)
        ,symbol(sy)
        ,myArray(myArr)
        ,myShips(Ship)
    {cout << "eimai o 'ship' 2" << endl; }
     ~ship() {}
    int get_x();
    int get_y();
    float get_max_resistance();
    float get_current_resistance();
    int get_speed();
    float get_reserve_treasure();
    char get_symbol();
    void set_x(int pos_x);
    void set_y(int pos_y);
    void set_max_resistance(float maxres);
    void set_current_resistance(float curres);
    void set_speed(int sp);
    void set_reserve_treasure(float restrea);
    void set_symbol(char sy);

    void movement();
    void operation();
};
int ship::get_x(){
    return x;
}
int ship::get_y(){
    return y;
}
float  ship::get_max_resistance(){
    return  max_resistance;
}
float ship::get_current_resistance(){
    return current_resistance;
}
int ship::get_speed(){
    return speed;
}
float ship::get_reserve_treasure(){
    return reserve_treasure;
}
char ship::get_symbol(){
    return symbol;
}

void ship::set_x(int pos_x){
    x = pos_x;
}
void ship::set_y(int pos_y){
    y = pos_y;
}
void ship::set_max_resistance(float maxres){
    max_resistance = maxres;
}
void ship::set_speed(int sp){
    speed = sp;
}
void ship::set_current_resistance(float curres){
    current_resistance = curres;
}
void ship::set_reserve_treasure(float restrea){
    reserve_treasure = restrea;
}
void ship::set_symbol(char sy){
    symbol = sy;
}

class pirate : public ship
{

   public:

    pirate(int posx, int posy, float mr, float cr, float rt, int spe, char sym, map (&Array)[N] [M],vector<ship> &Ship ):ship(posx,posy,mr,cr,rt,spe,sym,Array,Ship){
cout << "eimai o 'pirate' 1" << endl;


    // ship(90,90,1,50,'@',Array,Ship) {//vector<vector<map> > Array, vector<vector<ship> > Ship)     {}
};

Hope you can help

1 Answers1

0

Looking through this code, did you create a custom definition for map? Otherwise, if you are trying to create an [N][M] array of Map objects, you are missing the type declaration of map. e.g. map<int,string> If you are trying to use map as a multidimensional array, this is not what std::map is for. Map is a generic container for storing key/value pairs.

  • the map (&myArray)[N][M] has some info about the weather,port and treasure in every spot. I have made a class "map" and a constructor as well. But i didn't upload it because i didn't though that can be relevant. – Ερωτόκριτος Δάμπασης Jan 01 '15 at 11:30
  • @AlanStokes yeah I wanted to add it as a comment but I don't have enough reputation to comment on posts yet. I know it goes against the guidelines but I just felt the need to clarify. – chrisplusplus Jan 02 '15 at 04:56