使用的typedef时,我的问题是有关模板的模板,映射和迭代器。
免责声明:
请接受我的道歉,我是新来的C++和我的词汇量是非常有限的,因此我无法用言语准确地描述了我的问题。我附上我的代码。我希望它能解释更多。
我的代码
我有两个类型的功能,设置和GET:
template<class T>
struct SetFunctionClass
{
typedef boost::function<void(T)> SetFunction;
};
template<class G>
struct GetFunctionClass
{
typedef boost::function<G()> GetFunction;
};
,我可以像这样访问:
void setX (int i)
{
std::cout<< "Ciao"<< i*2 << std::endl;
}
float getA()
{
return 0.1;
}
...
SetFunctionClass<int>::SetFunction fun = &setX;
GetFunctionClass<float>::GetFunction fun2 = &getA;
...
直到这里的一切或多或少的罚款,但我希望能够插入这样的功能在容器类,使用代码类似于:
FunctionHandler<SetFunctionClass<int>::SetFunction> myIntSetFunctionsHandler ;
FunctionHandler<GetFunctionClass<float>::GetFunction> myFloatGetFunctionsHandler ;
std::string methodName("setX");
myIntSetFunctionsHandler.insert(methodName, fun);
std::string methodName2("getA");
myFloatGetFunctionsHandler.insert(methodName2, fun2);
,这是我的容器类:
template<template <typename > class FunctionClass>
class FunctionHandler
{
private:
typedef std::map<std::string, FunctionClass > Function_Map;
typedef typename std::map<std::string, FunctionClass >::iterator Function_Map_Iter;
Function_Map map;
public:
FunctionHandler(void);
virtual ~FunctionHandler(void);
void insert(std::string name, FunctionClass fun)
{
std::string funName(name);
map.insert(funName, fun);
}
FunctionClass find(const std::string& name)
{
std::auto_ptr<std::string> methodName(name);
Function_Map_Iter iter = map.find(*methodName.get());
if (iter == map.end())
throw std::runtime_error("Inexistent method: " + *methodName.get());
return iter->second;
}
};
我的问题: 我的问题在于我模板类FunctionHandler方式:
template<template <typename > class FunctionClass>
许多编译错误上模板
并且还在模板参数在这里: 注意:Eclipse不接受这个签名
FunctionHandler<SetFunctionClass<int>::SetFunction> myIntSetFunctionsHandler;
问: 能否请你解释一下我如何创建一个具有为模板另一个模板模板类???
EDITED
谢谢!
**这是什么问题?你有编译错误吗?运行时错误?别的东西? – 2014-09-24 14:27:38
@ n.m。如何创建一个作为另一个模板模板的模板类 – 2014-09-24 14:28:32
@NeilKirk这是一个太宽泛的问题。我不知道从哪里开始。 “解压缩并插入计算机...”显示**特定**问题。 “我正在做XYZ,我希望ABC能够发生,但是我得到了IJK”。 – 2014-09-24 14:31:58