mercredi 11 septembre 2019

how to initialize std::map as a class member

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

Aucun commentaire:

Enregistrer un commentaire