C++ Inserting a class into a map container
I have a map in C++ and I wish to input my class as the value, and a string as the key.
When I try to, I get an error 'Scene_Branding' : illegal use of this type as an expression
I get an illegal use of this type as an expression, and I can't seem to find out why. Here is some code.
string CurrentScene = "Scene_Branding";
map<string, Scene> Scenes;
Scenes.insert(std::make_pair("Scene_Branding", Scene_Branding)); //<-- Illegal Error parameter 2
and here is Scene Branding header..
#ifndef Scene_Branding_H
#define Scene_Branding_H
#include "Scene.h"
#include <iostream>
#include 开发者_如何学运维<string>
class Scene_Branding : Scene
{
public:
Scene_Branding();
~Scene_Branding();
void Draw();
};
#endif
and here is Scene header..
#ifndef Scene_H
#define Scene_H
#include <iostream>
#include <string>
class Scene
{
public:
Scene();
~Scene();
virtual void Draw();
};
#endif
and here is there cpp files.
Scene cpp.
#include "Scene.h"
Scene::Scene()
{
}
Scene::~Scene()
{
}
void Scene::Draw(){
std::cout << "Hey";
}
Scene_Branding cpp
#include "Scene_Branding.h"
Scene_Branding::Scene_Branding()
{
}
Scene_Branding::~Scene_Branding()
{
}
void Scene_Branding::Draw()
{
std::cout << "Drawing from Scene_branding";
}
First, don't store objects themselves in the map, store pointers to your objects.
Second, you need to give an instance of Scene_Branding to std::make_pair, not the class itself.
EDIT: Here's how you go about storing pointers:
string CurrentScene = "Scene_Branding";
map<string, Scene*> Scenes;
Scenes.insert(std::make_pair("Scene_Branding", new Scene_Branding()));
But, since you asked this type of question, i recommend you read a good c++ book for further grasping of concepts like pointers.
Try:
Scenes.insert(std::make_pair("Scene_Branding", Scene_Branding()));
I think you don't want to do that.
- there is no runtime type-mapping in C++, you store objects, not types.
- you cannot store polymorphic types in STL containers, use
boost::ptr_map
instead if it is your wish
So, the "new" code:
class Scene
{
public:
virtual ~Scene(); // Virtual Destructor, it's a base class
virtual Scene* clone() const = 0; // Polymorphic construction
private:
// whatever you wish
};
class Scene_Branding: public Scene
{
public:
virtual Scene_Branding* clone() const { return new Scene_Branding(); }
};
And the new way to store those:
const std::string SceneBrandingKey = "Scene_Branding";
typedef boost::ptr_map<std::string, Scene> scenes_type;
scenes_type Scenes;
Scenes.insert(SceneBrandingKey, new Scene_Branding());
And you can use it thusly:
Scenes["Scene_Branding"].process(); // Note: use '.' not '->'
The nice thing about Boost Pointer Container is that it's been meant for polymorphic types, with exception safety and all, and yet mimics the behavior / interface of the STL so that you are not lost :)
You need pointers in class when use generic map containers else, the result is possible a new object empty of your class... with pointers works!
std::map<std::string, Type*> map_;
Insert
map_["key"] = *Type;
NOTE: if -fpermisive flag to compiler is not set you need pass *Type not const when insert in the map_ container.
Best Regards,
Javier,
精彩评论