我有一个类对象和一个typedef:
class Object
{
private:
long int id;
public:
Object(void);
~Object(void) {};
long int get_id(void);
};
typedef map<long int, Object> obj_map;然后我有了这个类应用程序,它有一个obj_map和object_counter:
class App
{
public:
static ALLEGRO_DISPLAY *display;
static ALLEGRO_EVENT_QUEUE *event_queue;
static ALLEGRO_TIMER *timer;
static ALLEGRO_EVENT e;
static bool running;
static bool redraw;
static key_map key_states;
static obj_map objects; // << Here.
static long int object_counter; // << Here.
const char *window_title;
int screen_width;
int screen_height;
float FPS;
act event_scenes;
act visual_scenes;
ALLEGRO_COLOR background_color;
static ALLEGRO_EVENT event();
static ALLEGRO_EVENT_TYPE event_type();
static void shut_down();
App(int screen_width, int screen_height, const char *window_title = "Joy++ Application", float FPS = 30);
~App() {};
int init_all();
void register_all();
void check_key_states();
void init_key_states();
void run();
void destroy_all();
void add_event_scene(Scene scene);
void add_visual_scene(Scene scene);
void remove_event_scene(Scene scene);
void remove_visual_scene(Scene scene);
long int get_object_count();
unsigned int get_random_int(unsigned int min, unsigned int max);
void set_key_state(int al_key, string key_name, bool state);
void set_background_color(int r, int g, int b);
};正如你所看到的,这个想法是将应用程序中的每一个对象存储在一个id下面,在一个地图中。但是,我希望这发生在每一个物体的创建时刻。下面是构造函数的定义:
Object::Object()
{
App::object_counter += 1;
this->id = App::object_counter;
App::objects[this->id] = this; // Problem.
}错误:
G:\Development\Game-Development\CB\Joy-Plus-Plus\app.cpp|26|error: no match for 'operator=' (operand types are 'std::map<long int, Object>::mapped_type {aka Object}' and 'Object* const')|在创建对象时,我如何将每个对象的实例本身传递给外部映射?
发布于 2015-06-19 06:52:22
如果对象具有值语义,那么只分配*this (对象)而不是this。
另一方面,如果身份很重要,那么就构建到对象的映射*(或者,更好的std::shared_ptr),然后分配将按原样工作。
发布于 2015-06-19 06:52:01
因为这是一个指针类型->尝试*这个
您想要在地图中存储副本还是只存储对对象的引用?
https://stackoverflow.com/questions/30931694
复制相似问题