c++ “no matching function for call to” error with structure

前端 未结 2 539
一整个雨季
一整个雨季 2020-12-21 13:07

I have C++ code that maps GUID(unsigned long) to structure.

#include 
#include 
#include 

typedef unsigned long GU         


        
相关标签:
2条回答
  • 2020-12-21 13:20

    The problem is that when you say:

     pluginDB[1]
    

    you try to create an entry in the map (because [1] does not exist) and to do that as Jason points out, you need a default constructor. However, this is NOT a general requirement of standard library containers, only of std::map, and only of operator[] for std::map (and multimap etc.), which is a good reason why IMHO operator[] for maps et al should be done away with - it is far too confusing for new C++ programmers, and useless for experienced ones.

    0 讨论(0)
  • 2020-12-21 13:23

    Any objects you place in a STL container initialized with an initial number of objects (i.e., you're not initializing an empty container) must have at least one default constructor ... yours does not. In other words your current constructor needs to be initialized with specific objects. There must be one default constructor that is like:

    PluginInfo();
    

    Requiring no initializers. Alternatively, they can be default initializers like:

    PluginInfo(GUID _guid = GUID(), 
               std::string _name = std::string(), 
               Function _function = Function()): 
               guid(_guid), name(_name), function(_function) {}
    
    0 讨论(0)
提交回复
热议问题