Reputation:
How can I initialize std::map
variable in my class.
Currently my code is not compiling and i am getting error.
Error C3376 'SumCommandInterface::call_function': only static data member templates are allowed
Code:
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 ));
}
This should be the defination
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 }
};
Upvotes: 0
Views: 299
Reputation: 37647
Ok now I see the problem.
It should go like this:
// 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",
...
},
};
Note to std::function
you have to provide function which matches declaration inside std::function
and your SumCommandInterface::stringFunction
reqiried implicit argument this
(regular methods are invoked for specific object).
So as a solution I used lambda expression which captures missing this
.
Also there is a problem with template. You can't declare class field as template. Only methods can be a template or whole class can be a template, not a field.
Here is minimal complete demonstration that it works: https://wandbox.org/permlink/PAMGV04fr0BN4ueb I'm unable to quickly fix your code.
Upvotes: 3