在类构造函数中向向量添加值时的变量范围

时间:2010-05-07 23:27:12

标签: c++ constructor vector

我有一个级别类和一个Enemy_control类,它基于一个以Enemys作为值的向量。 在我的关卡构造函数中,我有:

Enemy tmp( 1200 );
enemys.Add_enemy( tmp ); // this adds tmp to the vector in Enemy_control

enemys是Enemy_control类型的变量。在这些语句抱怨级别和敌人控制以及敌人中的一些析构函数问题后,我的程序崩溃了。

Level1::Level1() : Levels()
{
bgX1 = -60; // -60
bgX2 = -130; // -110
bgX3 = -240; // -
bgY=0; // is this used anymore?

// characterY=330;
max_right_movement=500;
max_left_movement=300;

 // More test
jump_max = 110;
player_current_y = 340;
jump_spd = 4;
player_current_floor_y = 340;
//CONST_LEVEL1_MAIN_Y = new int( 340 );

scrolling = true;
scrolling_right = true; // this var is in levels

level_alive = true;
restart_level = false;

player_level_x = 300;
player_screen_x = 300;

level_end_point = 1035 * 10;
level_start_point = 0;

// create enemys in the level
Enemy tmp( 1200 );
enemys.Add_enemy( tmp );


//    tmp.Set_enemy( 4600 );
//    enemys.Add_enemy( tmp );
scoreTitle = new MyText(25);
score = new MyText(25);
high_score=0;
//onblock=0;

load( "grafx/level1/clouds.png", "grafx/level1/mountain.png", "grafx/level1/ground.png", "sounds/level1music.ogg" );
}

and enemy.h:

/* Enemy.h
* obg
* 1-13-10
*/

#ifndef ENEMY_H
#define ENEMY_H

#include "Character.h"
#include <vector>

class Enemy : public Character
{
    private:
        int enemy_speed,
            DRAW_X,
            spawn_x,
            distance_to_enemy,
            frame_left,
            frame_right,
            death_frame_x,
            death_frame_y;

        int bullet_fire_rate;

        bool following_player;

    private:
        void Draw_enemy_going_right( SDL_Surface *video_surface );
        void Draw_enemy_going_left( SDL_Surface *video_surface );
        void Draw_death( int bgX3, int y, SDL_Surface *video_surface );

        void Move_frame_left();
        void Move_frame_right();

        void Draw_me( int bgX3, int y, SDL_Surface *video_surface, int player_level_x, int player_screen_x );

        void Fire_bullet();

    public:
        Enemy( int spawn_x );
        ~Enemy();

        //void Set_enemy( int spawn_x );
        void Draw( int bgX3, int y, SDL_Surface *video_surface, int player_level_x, int player_screen_x );
        bool Following_player();

};

class Enemy_control
{
    private:
        vector< Enemy > enemy_vector; // change this to a vector

    public:
        Enemy_control();
        ~Enemy_control();

        void Add_enemy( Enemy a_enemy );
        void Draw( int bgX3, int y, SDL_Surface *video_surface, int player_level_x, int player_screen_x );
        //void Free_list_from_memory();
};

敌人构造函数:

Enemy :: Enemy(int spawn_x):Character() {

character_image.load(“grafx / enemy1.gif”);

health = 400;
damage = 20;

DRAW_X = spawn_x;
this->spawn_x = spawn_x;

distance_to_enemy = 230;
enemy_speed = 3;
frame_left = 0;
frame_right = 0;
death_frame_x = 390;
death_frame_y = 0;

following_player = false;
bullet_fire_rate = 0;

sprite_info.w = 63;
sprite_info.h = 74;
cout << "enemy built please call Set_enemy()\n";

}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

正如alread指出的那样,对你的问题的描述并不十分准确。令我感到奇怪的事情之一是为什么你的任何类的析构函数被调用。查看代码后,很明显 Enemy tmp 是一个本地实例,在离开Level构造函数后会被清除。 Add_Enemy()函数接收此本地对象的副本。由于您没有提供复制构造函数,因此不会在复制构造函数中执行对character_image.load( "grafx/enemy1.gif" );的调用,因此您存储的Enemy对象将没有图像。

也许你应该考虑这样的事情:

Enemy* tmp = new Enemy( 1200 ); // dynamic allocation of Enemy
enemey.Add_enemy( tmp );       

如果你这样做,你需要改变你的向量来存储指向敌人对象的指针 比如std::vector< Enemy* >敌人。不要忘记在Enemy_control的析构函数中删除动态分配的对象。