C++ 将类插入到地图容器中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2281420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:51:28  来源:igfitidea点击:

C++ Inserting a class into a map container

c++classinheritancemap

提问by Fouf

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 expressionI get an illegal use of this type as an expression, and I can't seem to find out why. Here is some code.

我有一个 C++ 映射,我希望输入我的类作为值,并输入一个字符串作为键。当我尝试时,我收到一个错误,'Scene_Branding' : illegal use of this type as an expression我将这种类型作为表达式非法使用,但我似乎无法找出原因。这是一些代码。

 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.

这里有 cpp 文件。

Scene cpp.

场景 cpp。

#include "Scene.h"

Scene::Scene()
{

}
Scene::~Scene()
{

}
void Scene::Draw(){
 std::cout << "Hey";
}

Scene_Branding cpp

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";
}

回答by erelender

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.

其次,您需要将 Scene_Branding 的实例提供给 std::make_pair,而不是类本身。

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.

但是,既然你问了这类问题,我建议你阅读一本好的 C++ 书,以进一步掌握指针等概念。

回答by Alexander Gessler

Try:

尝试:

Scenes.insert(std::make_pair("Scene_Branding", Scene_Branding()));

回答by Matthieu M.

I think you don't want to do that.

我想你不想那样做。

  1. there is no runtime type-mapping in C++, you store objects, not types.
  2. you cannot store polymorphic types in STL containers, use boost::ptr_mapinstead if it is your wish
  1. C++ 中没有运行时类型映射,你存储的是对象,而不是类型。
  2. 你不能在 STL 容器中存储多态类型,boost::ptr_map如果你愿意,可以改用

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 Containeris 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 :)

约的好处升压指针集装箱是,它已经意味着多态类型,具有异常安全和所有,然而模仿STL的行为/接口,因此你不会丢失:)

回答by Javier Gutiérrez-Maturana Sánc

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.

注意:如果未设置编译器的 -fpermisive 标志,则在插入 map_ 容器时需要传递 *Type not const。

Best Regards,

此致,

Javier,

哈维尔,