我认为我的问题与模板相关,但我只是不知道。我收到如下错误:
error: conversion from '__gnu_cxx::__normal_iterator<FxStreamable* (***)(), std::vector<FxStreamable* (**)(), std::allocator<FxStreamable* (**)()> > >' to non-scalar type '__gnu_cxx::__normal_iterator<std::pair<FxClassID, FxStreamable* (*)()>*, std::vector<std::pair<FxClassID, FxStreamable* (*)()>, std::allocator<std::pair<FxClassID, FxStreamable* (*)()> > > >' requested
和
no match for 'operator!=' in 'itera1 != ((FxPairRegistry<FxClassID, FxStreamable* (*)()>*)this)->FxPairRegistry<FxClassID, FxStreamable* (*)()>::mRegistryList. std::vector<_Tp, _Alloc>::end [with _Tp = FxStreamable* (**)(), _Alloc = std::allocator<FxStreamable* (**)()>]()'
代码看起来像
for (iter itera1 = mRegistryList.begin(); itera1 != mRegistryList.end(); itera1++)
{
if ((*itera1).first == id)
{
return (*itera1).second;
}
}
谁能解释一下出了什么问题吗?
更新:mRegistryList 已定义 vector<registeredObject *> mRegistryList;
更新:itera 被定义 typedef typename std::vector<pair<identifier,registeredObject> >::iterator iter;
更新3:
template <class identifier,class registeredObject> registeredObject FxPairRegistry<identifier,registeredObject>::GetEntry(identifier id, FxBool assertValue)
{
for (std::vector<registeredObject *>::iterator itera1 = mRegistryList.begin(); itera1 != mRegistryList.end(); itera1++)
{
if ((*itera1).first == id)
{
return (*itera1).second;
}
}
if (assertValue)
ASSERT_MSG(0,"Entry not found in the registry");
return NULL;
}
最佳答案
您的迭代器类型与mRegistryList
不匹配vector
的迭代器类型。
迭代器:std::vector<std::pair<FxClassID, FxStreamable* (*)()> >::iterator
容器:std::vector<FxStreamable* (**)()>
编辑:响应更新:
使用vector<registeredObject *>::iterator
- 不是你的其他不相关的迭代器。
为了迭代 vector<X>
容器,你需要一个vector<X>::iterator
不是vector<SomethingElse>::iterator
编辑:响应新更新:
for (typename std::vector<registeredObject *>::iterator itera1 = mRegistryList.begin(); itera1 != mRegistryList.end(); itera1++)
^^^^^^^^
由于此代码位于模板中,因此编译器不知道 std::vector<registeredObject *>::iterator
是一种类型 - 您必须通过前缀 typename
告诉它将其视为一种类型
关于C++ 模板帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5709178/