【发布时间】:2011-03-31 07:11:18
【问题描述】:
我目前正在努力编译以下代码。首先是包含一个带有方法模板的类的头文件:
// ConfigurationContext.h
class ConfigurationContext
{
public:
template<typename T> T getValue(const std::string& name, T& default) const
{
...
}
}
我想在其他地方这样调用这个方法:
int value = context.getValue<int>("foo", 5);
我收到以下错误:
error: no matching function for call to 'ConfigurationContext::getValue(const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, int)'
我检查了明显的错误,例如缺少包含和类似的东西。但一切似乎都是正确的。我尝试像这样删除模板类型参数的传递引用:
template<typename T> T getValue(const std::string& name, T default) const ...
然后它编译没有任何错误并且运行良好,但我仍然想在这里传递一个参考......
有谁知道这里发生了什么以及如何进行这项工作?
【问题讨论】:
标签: c++ templates methods pass-by-reference