gpt4 book ai didi

STL - C++ "no matching function for call to"结构错误

转载 作者:行者123 更新时间:2023-12-01 15:54:25 26 4
gpt4 key购买 nike

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

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

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

/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 容器中的任何对象(即,您没有初始化一个空容器)都必须至少有一个默认构造函数...您的没有。换句话说,您当前的构造函数需要使用特定对象进行初始化。必须有一个默认构造函数,如下所示:

PluginInfo();

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

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

关于STL - C++ "no matching function for call to"结构错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5708514/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com