从指针列表调用时,STL列表push_back崩溃

时间:2015-01-05 13:41:58

标签: c++ list methods crash push-back

我遇到的另一个问题出现了问题。我正在使用自定义类(我已实现的类)的指针列表,当我调用此方法时程序崩溃,但是,如果我不使用指针列表,它可以正常工作。这是程序崩溃的功能:

void enemy_spawner()
{
    Point2D enemy_pos = Point2D(rand() % (SCREEN_WIDTH - 20) + 20, -20);
    Point2D enemy_vel = Point2D(0, rand() % 4 + 1);

    Sprite* enemy = new Sprite(enemy_pos, enemy_vel, enemy_image, enemy_info);
    //Kamikaze* enemy = new Kamikaze(enemy_pos, enemy_vel, 0, enemy_image, enemy_info);

    if (enemy_group.size() < MAX_ENEMIES)
    {
        // Program crashes here
        enemy_group.push_back(enemy);
    }

}

enemy_group是一个全局变量,定义如下:

std::list<Sprite*> enemy_group;

My Sprite类如下(我已经读过我需要有正确定义的拷贝构造函数等)

class Sprite
{
private:
    Point2D sp_pos;
    Point2D sp_vel;
    SDL_Surface* sp_img;
    Point2D sp_center;
    Point2D sp_size;
    double sp_radius;
    bool sp_animated;
    int sp_frames;
    int sp_cur_frame;

public:
    Sprite() {}
    Sprite(Point2D pos, Point2D vel, SDL_Surface *img, ImageInfo info, bool animated = false, int frames = 0);
    virtual void draw(SDL_Surface* screen);
    virtual void update();
    void setInfo (ImageInfo info);
    void setPos( Point2D pos ) { sp_pos = pos; }
    void setVel( Point2D vel ) { sp_vel = vel; }
    void setImg (SDL_Surface* img) { sp_img = img; }
    void setNextFrame() { sp_cur_frame++; }
    void setFrame( int frame ) { sp_cur_frame = frame; }
    void setAnimated(bool animated) { sp_animated = animated; }
    void changeVelX (int c) { sp_vel.setX(c);}
    void changeVelY (int c) { sp_vel.setY(c);}
    void changePosX (int c) { sp_pos.setX(c);}
    void changePosY (int c) { sp_pos.setY(c);}
    SDL_Surface* getImg() { return sp_img; }
    Point2D getPos() { return sp_pos; }
    Point2D getVel() { return sp_vel; }
    Point2D getCenter() { return sp_center; }
    Point2D getSize() { return sp_size; }
    double getRadius() { return sp_radius; }
    int getCurFrame() { return sp_cur_frame; }
    int getFrames() { return sp_frames; }
    bool collide(Sprite &another_sprite);
    virtual ~Sprite() {}

    Sprite& operator=(Sprite other) {
        std::swap(sp_pos, other.sp_pos);
        std::swap(sp_vel, other.sp_vel);
        std::swap(sp_img, other.sp_img);
        std::swap(sp_center, other.sp_center);
        std::swap(sp_size, other.sp_size);
        std::swap(sp_radius, other.sp_radius);
        std::swap(sp_animated, other.sp_animated);
        std::swap(sp_frames, other.sp_frames);
        std::swap(sp_cur_frame, other.sp_cur_frame);
        return *this;
    }

    Sprite(const Sprite &obj)
    {
        sp_pos = obj.sp_pos;
        sp_vel = obj.sp_vel;
        sp_img = new SDL_Surface;
        *sp_img = *obj.sp_img;
        sp_center = obj.sp_center;
        sp_size = obj.sp_size;
        sp_radius = obj.sp_radius;
        sp_animated = obj.sp_animated;
        sp_frames = obj.sp_frames;
        sp_cur_frame = obj.sp_cur_frame;
    }
};

如果重要,我正在使用gcc编译器(实际上我的IDE是代码块)。问题是,为什么程序会崩溃?我该如何解决这个问题?

提前致谢

0 个答案:

没有答案