c ++“调用不匹配功能”结构错误 [英] c++ "no matching function for call to" error with structure

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

问题描述

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

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

typedef unsigned long GUID;

枚举功能{
ADDER = 1,
SUBTRACTOR = 2,
MULTIPLIER = 3,
SQUAREROOT = 4
};

struct PluginInfo
{
GUID guid;
std :: string name;
函数函数;

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);

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

当我编译它,我收到错误消息。


/ 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 =
std :: allocator>]':
mockup_api.cpp:58:从
中实例化
/usr/include/c++/4.2.1/bits/stl_map。 h:350:
错误:没有匹配的函数调用
到'PluginInfo :: PluginInfo()'
mockup_api.cpp:29:注意:候选人
是:PluginInfo :: PluginInfo(GUID,
std :: string,Function)
mockup_api.cpp:24:note:

PluginInfo :: PluginInfo(const
PluginInfo&)


可能有什么问题?

解决方案

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

  PluginInfo(); 

不需要初始化器。或者,它们可以是默认的初始化器,如:

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


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

What might be wrong?

解决方案

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天全站免登陆