调整对象矢量大小时出错

时间:2014-12-31 22:37:48

标签: c++ object vector resize

我正在尝试创建一个对象向量,但我有一些问题。我无法将19个对象推回到向量中,因为它显示了bad_alloc的错误消息。

我尝试使用resize()或reserve()来调整我的矢量大小,但仍然没有。

对于resize(),我读到你需要提供2个参数来调整vector的大小。但是仍然没有。 当我尝试在没有push_back的情况下使用它时,它会显示错误:在')之前预期的primary-expression;令牌。

#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() << " ";
  }

}

这是剩下的代码,可以帮助您理解:

  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)     {}
};

希望你能帮忙

1 个答案:

答案 0 :(得分:0)

查看此代码,您是否为map创建了自定义定义?否则,如果您尝试创建Map对象的[N] [M]数组,则缺少map的类型声明。例如map<int,string> 如果您尝试将map用作多维数组,那么这不是std :: map的用途。 Map是用于存储键/值对的通用容器。