到目前为止,这么好。但是,出现了一些问题。另一个奇怪的编译器错误:调用一个给出未定义参考的模板函数
首先,是当我调用如下
const bool bppExists = CheckMapForExistingEntry< std::string, int >(mConfigValues, "bpp");
我得到如下:
error: undefined reference to `bool CheckMapForExistingEntry<std::string, int>(std::map<std::string, int, std::less<std::string>, std::allocator<std::pair<std::string const, int> > > const&, std::string const&)'
在有些情况下这种情况正在发生的其他三个实例。功能如下所示:
宣言
template < class Key, class Value >
bool CheckMapForExistingEntry(const std::map< Key, Value >& map, const std::string& key);
定义
template < class Key, class Value >
bool CheckMapForExistingEntry(const std::map< Key, Value >& map, const std::string& key)
{
typename std::map< Key, Value >::iterator it = map.lower_bound(key);
bool keyExists = (it != map.end && !(map.key_comp() (key, it->first)));
if (keyExists)
{
return true;
}
return false;
}
因此,这里发生了什么?我有包含该函数声明的头文件,但它仍然不起作用。根据this,我应该忽略模板参数的值,只是通过key_type
,但是拒绝工作。
例如:
CheckMapForExistingEntry<std::string>(someMap, "somekey"); //error
源文件;声明在标题中。 – zeboidlund 2012-01-28 00:10:57