“类 std::map 在没有模板参数的情况下使用” 错误
我不得不说我不是使用 STL 的专家。 这是我的问题,我有一个名为 LdapClientManager 的类,它维护许多由 ID 管理的 LDAP 客户端。 保存 LdapClients 的容器被声明为成员变量,即
typedef std::map<int, LdapClient *> LdapClientMap;
LdapClientMap _ldapClientMap;
以下函数无法编译并出现错误:
LdapClient * LdapClientManager::getLdapClient(unsigned int templateID)
{
// Do we have an LdapClient
LdapClientMap::const_iterator it = _ldapClientMap.find(templateID);
if (it == std::map::end) {
// no existing client, lets create it
LdapClient * ldapClient = new LdapClient();
if (ldapClient == NULL) {
// TODO: handle out of memory condition
}
_ldapClientMap[templateID] = ldapClient;
return ldapClient;
}
return it->second;
}
不幸的是,我在编译时收到以下错误,这是什么意思。 我目前google还没有找到解决方案。
LdapClientManager.cc:在成员函数 LdapClient* 中 LdapClientManager::getLdapClient(unsigned int)': LdapClientManager.cc:33:
模板类 std::map' 在没有模板参数的情况下使用
I'd have to say I'm no expert on using the STL. Here's my problem, I have a class Called LdapClientManager which maintains a number of LDAP clients that are managed by ID. The container holding the LdapClients is declared as a member variable i.e.
typedef std::map<int, LdapClient *> LdapClientMap;
LdapClientMap _ldapClientMap;
The following function fails to compile with the error:
LdapClient * LdapClientManager::getLdapClient(unsigned int templateID)
{
// Do we have an LdapClient
LdapClientMap::const_iterator it = _ldapClientMap.find(templateID);
if (it == std::map::end) {
// no existing client, lets create it
LdapClient * ldapClient = new LdapClient();
if (ldapClient == NULL) {
// TODO: handle out of memory condition
}
_ldapClientMap[templateID] = ldapClient;
return ldapClient;
}
return it->second;
}
Unfortunately I get the following error at compile time, what does it mean. I haven't found a solution in google as yet.
LdapClientManager.cc: In member function LdapClient*
template class std::map' used without template parameters
LdapClientManager::getLdapClient(unsigned int)':
LdapClientManager.cc:33:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将
std::map::end
替换为_ldapClientMap.end()
。另外,
new
永远不会返回 0,如果分配失败,它会抛出异常。请注意,程序可以变得更短。
Replace
std::map::end
with_ldapClientMap.end()
.Also,
new
never returns 0, it throws an exception if the allocation fails.Note that the program can be made much shorter.
它的意思正是它所说的意思。
std::map
是一个类模板。 它本身并不是一个类。 它需要模板参数,就像您在定义 LdapClientMap 类型时使用的那样。 后来,你说std::map::end
,编译器说它也需要参数。但您可能指的是
_ldapClientMap.end()
。 每张地图都有自己的终点;end
不是静态函数,因此您需要在实例上调用它。 如果它是静态的,您将需要提供模板参数,就像定义类型时一样:std::map::end
。It means exactly what it says it means.
std::map
is a class template. It is not a class in and of itself. It needs template parameters, like you used when you defined theLdapClientMap
type. Later, you saystd::map::end
, and the compiler says that needs parameters, too.But you probably meant
_ldapClientMap.end()
. Each map has its own end;end
is not a static function, so you need to call it on an instance. If it were static, you would have needed to provide template parameters, just like when you defined the type:std::map<int, LdapClient*>::end
.std::map::end() 是容器实例的成员函数,而不是通用值,因此您需要根据 _ldapClientMap.end() 检查 std::map::find() 的结果。
改进代码的另外一些建议:
std::map::end() is a member function of the container instance and not a universal value, so you'll need to check the result of std::map::find() against _ldapClientMap.end().
Another couple of suggestions to improve the code:
您应该避免使用以下划线开头的名称。 从技术上讲,这是未定义的行为,即使编译器允许它,因为使用它会与当前或将来的保留名称发生冲突。
You should avoid using names with a leading underscore. Technically it is undefined behavior, even if the compiler allows it because by using it you conflict with current or future reserved names.