如何在课堂上初始化std::map
变量。
当前我的代码未编译,并且出现错误。
Error C3376 'SumCommandInterface::call_function': only static data member templates are allowed
代码:
class SumCommandInterface
{
private:
std::string stdstrCommand;
std::map < std::string, PluginTypeLookUp > lookUpPluginMap;
// I want to initialize this member
template<typename plugin>
std::map<std::string, std::function<void(plugin* plug, const std::string &, const std::string &)>> call_function;
public:
SumCommandInterface();
int receiveCommand(std::string stdtsrCommand , const QModelIndex & RootIndex , TreeModel *myModel);
bool getTreeItem(const std::string &stdstrName , const QModelIndex & RootIndex, TreeModel *myModel , TreeItem** item );
template<typename plugin, typename fieldType>
inline void setFontPluginParam(plugin* geom, std::string paramName, fieldType val);
template<typename plugin, typename fieldType>
inline void setTexturePluginParam(plugin* geom, std::string paramName, fieldType val);
template<typename plugin>
void stringFunction(plugin* plug , const std::string& name, std::string stdstr);
template<typename plugin>
void intFunction( plugin* plug, const std::string& name, std::string stdstr);
template<typename plugin>
void floatFunction(plugin* plug , const std::string& name, std::string stdstr);
template<typename plugin>
void boolFunction(plugin* plug , const std::string& name, std::string stdstr);
};
template<typename plugin, typename fieldType>
inline void SumCommandInterface::setTexturePluginParam(plugin* plug, std::string paramName, fieldType val)
{
CommandInterfaceTexture commandInterface;
Sum_Texture_2D *texture = plug;
commandInterface.SetValue< Sum_Texture_2D >(texture, paramName, val);
}
template<typename plugin>
void SumCommandInterface::stringFunction(plugin* plug, const std::string& name, std::string stdstr)
{
setFontPluginParam<Geometry , std::string>(plug, name , stdstr)
}
template<typename plugin>
void SumCommandInterface::intFunction(plugin* plug, const std::string& name, std::string stdstr )
{
setFontPluginParam<Geometry, std::string>(plug, name, std::stoi(stdstr));
}
template<typename plugin>
void SumCommandInterface::floatFunction(plugin* plug, const std::string& name, std::string stdstr)
{
setFontPluginParam<Geometry, std::string>(plug, name, std::stof(stdstr));
}
template<typename plugin>
void SumCommandInterface::boolFunction(plugin* plug, const std::string& name, std::string stdstr)
{
setFontPluginParam<Geometry, std::string>(plug, name, (stdstr == '0' ? false : true ));
}
这应该是定义
template<typename plugin>
std::map<std::string, std::function<void(plugin* plug, const std::string &, const std::string &)>> call_function =
{
{"TEXT", SumCommandInterface::stringFunction },
{"USESHADOW", SumCommandInterface::boolFunction },
{"FONTSIZE", SumCommandInterface::intFunction },
{"SHADOWDISTANCE", SumCommandInterface::floatFunction },
{"SHADOWOPACITY", SumCommandInterface::floatFunction },
{"KERNING", SumCommandInterface::floatFunction }
};
答案 0 :(得分:4)
好,现在我看到了问题。
它应该像这样:
// template<typename plugin> // this is problem too - see below!
std::map<std::string, std::function<void(plugin* plug, const std::string &, const std::string &)>> call_function =
{
{
"TEXT",
[this](plugin* plug, const std::string &s, const std::string &s2)
{
SumCommandInterface::stringFunction(plug, s1, s2); };
}
},
{
"USESHADOW",
...
},
};
请注意std::function
,您必须提供与std::function
内的声明和您的SumCommandInterface::stringFunction
要求的隐式参数this
相匹配的函数(为特定对象调用常规方法)。 / p>
因此,作为解决方案,我使用了lambda表达式来捕获丢失的this
。
模板也有问题。您不能将类字段声明为模板。只有方法可以是模板,整个类都可以是模板,而不是字段。
这里仅是最小的完整示例说明了它的工作原理:https://wandbox.org/permlink/PAMGV04fr0BN4ueb 我无法快速修复您的代码。