本文介绍了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;
}

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

可能有什么问题?

解决方案

您放置在初始化对象数目的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.

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 ++“调用不匹配功能”结构错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:56