I would like to have objects add a pointer pointing to themselves into a map which is a member of another class.
Here is what I have so far:
Код: Выделить всё
// entitymanager.h
// Inside the public part of the EntityManager class
std::map AllMapEntities;
// main.cpp
EntityManager.AllMapEntities.insert({ 1, pObject1});
// mapentity.cpp
// constructor for MapEntity
MapEntity::MapEntity(){
EntityManager.AllMapEntities.insert({ meid, this });
}
However if I try to have the object add the pointer during its construction then it fails.
The errors in
Код: Выделить всё
mapentity.cppI don't see why the same syntax would work if called in
Код: Выделить всё
main.cppHere is a minimal example in one main file.
It won't compile on that site because it requires SDL.
However the errors are the same as in the previous code pieces.
Код: Выделить всё
#include
#include
#include
#include
#include
#include
class EntityManager {
private:
public:
std::map AllMapEntities;
EntityManager();
void CreateMapEntity();
};
class MapEntity {
private:
public:
// Constructor
MapEntity();
};
MapEntity::MapEntity() {
// This is the line with the 2 errors:
EntityManager.AllMapEntities.insert({ meid, this });
}
int main(int argc, char* argv[]) {
return 0;
}
Источник: https://stackoverflow.com/questions/781 ... onstructor
Мобильная версия