c++“没有匹配的调用函数"结构错误 [英] c++ "no matching function for call to" error with structure

查看:19
本文介绍了c++“没有匹配的调用函数"结构错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有将 GUID(unsigned long) 映射到结构的 C++ 代码.

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

#include <string>
#include <map>
#include <iostream>

typedef unsigned long GUID;

enum Function {
  ADDER = 1,
  SUBTRACTOR = 2,
  MULTIPLIER = 3,
  SQUAREROOT = 4
};

struct PluginInfo
{
    GUID guid;
    std::string name;
    Function function;

    PluginInfo(GUID _guid, std::string _name, Function _function) {guid = _guid, name = _name, function = _function;}
};

typedef std::map<GUID, PluginInfo> PluginDB;

PluginInfo temp1(1, "Adder", ADDER);
PluginInfo temp2(2, "Multiplier", MULTIPLIER);

PluginDB::value_type pluginDbArray[] = {
    PluginDB::value_type(1, temp1),
    PluginDB::value_type(2, temp2)
};

const int numElems = sizeof pluginDbArray / sizeof pluginDbArray[0];
PluginDB pluginDB(pluginDbArray, pluginDbArray + numElems);

int main()
{
    std::cout << pluginDB[1].name << std::endl;
}

当我编译它时,我收到了错误消息.

When I compile it, I got error message.

/usr/include/c++/4.2.1/bits/stl_map.h:在成员函数'_Tp&std::map<_Key, _Tp, _Compare,_Alloc>::operator[](const _Key&) [with _Key = long unsigned int, _Tp = PluginInfo, _Compare = std::less, _Alloc =标准::分配器>]':mockup_api.cpp:58: 实例化自这里/usr/include/c++/4.2.1/bits/stl_map.h:350:错误:没有匹配的调用函数到'PluginInfo::PluginInfo()'mockup_api.cpp:29:注意:候选人是:PluginInfo::PluginInfo(GUID,标准::字符串,函数)mockup_api.cpp:24: 注意:
插件信息::插件信息(常量插件信息&)

/usr/include/c++/4.2.1/bits/stl_map.h: In member function ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = long unsigned int, _Tp = PluginInfo, _Compare = std::less, _Alloc = std::allocator >]’: mockup_api.cpp:58: instantiated from here /usr/include/c++/4.2.1/bits/stl_map.h:350: error: no matching function for call to ‘PluginInfo::PluginInfo()’ mockup_api.cpp:29: note: candidates are: PluginInfo::PluginInfo(GUID, std::string, Function) mockup_api.cpp:24: note:
PluginInfo::PluginInfo(const PluginInfo&)

可能出了什么问题?

推荐答案

您放置在使用初始对象数量初始化的 STL 容器中的任何对象(即,您没有初始化一个空容器)必须至少有一个默认值构造函数......你的没有.换句话说,您当前的构造函数需要使用特定对象进行初始化.必须有一个默认构造函数,如下所示:

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) {}

这篇关于c++“没有匹配的调用函数"结构错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆