C++ 模板帮助

标签 c++ templates

我认为我的问题与模板相关,但我只是不知道。我收到如下错误:

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/

相关文章:

c++ - 在终端上执行 "make"时出错

c++ - 从可变类型列表中获取最大的类型

c++ - 函数模板 : Different specializations with type traits

c++ - 静态回调函数和非静态成员

c++ - 平滑运动参数

c++ - 使用 malloc() 时如何实现复制构造函数

c++ - 模板化检查是否存在类成员函数?

c++ - 为什么我需要在模板继承中使用范围解析?

c++ - 为什么将 const 类型的智能指针转换为类型有效的智能指针

c++ - 无序集,是否值得在插入前调用查找?