简体   繁体   English

C ++将std :: map值设置为结构实例的指针

[英]C++ set std::map value to pointer of struct instance

I have multiple devices, each of these has its own set of functions, each with different values and different variable types. 我有多个设备,每个设备都有其自己的功能集,每个功能都有不同的值和不同的变量类型。 I am trying to create a dataset that stores these variables in a simple way to access them while dynamically adding or removing these devices. 我试图创建一个数据集,以一种简单的方式存储这些变量,以便在动态添加或删除这些设备时访问它们。

My idea was to create a class that holds the device info, with a struct that can be used for the different functions. 我的想法是创建一个包含设备信息的类,该类具有可用于不同功能的结构。 To easily access the devices and functions, the device/function name would be stored in a map as the key and the pointer to the instance of the class/struct would be stored as the value of that key. 为了轻松访问设备和功能,设备/功能名称将作为键存储在映射中,而指向类/结构实例的指针将作为键的值存储。 For the devices map, this worked fine. 对于设备映射,这很好。 However, for the functions map, inside the class, I can't seem to get it to work. 但是,对于函数映射,在类内部,我似乎无法使其正常工作。 So... 所以...

1) What is the proper way of placing the struct pointer into the map? 1)将struct指针放置到映射中的正确方法是什么?

2) Is it a good idea to place a struct in a class? 2)将结构放置在类中是一个好主意吗?

3) I am fairly new to C++ so is there a better STL or method of doing this? 3)我对C ++还是很陌生,所以有没有更好的STL或这样做的方法?

This clip of code is located in an .hpp file 此代码剪辑位于.hpp文件中

std::map<std::string, device*> devices;
class device {
private:
    //Device info
    std::string devName;
    size_t devId;
    char state;

    std::map<std::string, struct func *func> functions; //---Error

    //Setup for multiple functions
    struct func{
        //Functions info
        std::string funcName;
        size_t funcId;
        char funcType;

    };

public:
//Functions to get device and function values

I run this code on Linux (Ubuntu 19.04), and I use Visual Studio Code as my IDE. 我在Linux(Ubuntu 19.04)上运行此代码,并使用Visual Studio Code作为我的IDE。 The error it gives me is: 它给我的错误是:

expected a '>' where *func is. 应该是* func所在的'>'。

Thanks guys, I was able to figure it out. 谢谢大家,我能够弄清楚。

std::map<std::string, device*> devices;
class device {
private:
   //Device info
   std::string devName;
   size_t devId;
   char state;

   //Setup for multiple functions
   struct func{
       //Functions info
       std::string funcName;
       size_t funcId;
       char funcType;
   };

   //Map of funcution Id's to pointers of function objects
   std::map <id, func*> functions;

  public:
  //Functions to get device and function values

It was a forward declaration error. 这是一个前向声明错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM