c++ - 为什么我不能用 const_iterator 调用模板基类构造函数?

标签 c++ templates inheritance c++11 const-iterator

<分区>

由于某些原因,下面的代码给出了错误Symbol 'TemplateBase' could not be resolved. :

template <typename T>
class TemplateBase
{
    TemplateBase(std::map<std::string, T>::const_iterator anIterator)
    { }
};

class SubClass : public TemplateBase<int>
{
    SubClass(std::map<std::string, int>::const_iterator anIterator) :
        TemplateBase<int>(anIterator) //Error: Symbol 'TemplateBase' could not be resolved.
    { }
};

奇怪的是,当我删除 ::const_iterator 时没有出现错误并且只有 std::map<std::string, int>遗迹:

template <typename T>
class TemplateBase
{
    TemplateBase(std::map<std::string, T> aMap)
    { }
};

class SubClass : public TemplateBase<int>
{
    SubClass(std::map<std::string, int> aMap) :
        TemplateBase<int>(aMap) //No error.
    { }
};

此外,下面的函数也没有报错,所以它看起来确实与模板基类调用与 const_iterator 的组合有关:

void function()
{
    std::map<std::string, int>::const_iterator anIterator;
    TemplateBase<int> aTemplateBase(anIterator); //No error
}

是否有一些我不知道的规则禁止使用 const_iterator 作为基类模板构造函数的参数?或者这是一个编译器错误?

我正在使用 C++11 在 Windows 7 上使用 MinGW 64 位 4.9.0 进行编译。

最佳答案

当您使用依赖于模板类型的嵌套类型时,您需要使用 typename 关键字:

TemplateBase(typename std::map<std::string, T>::const_iterator anIterator)
{ }

关于c++ - 为什么我不能用 const_iterator 调用模板基类构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23963634/

相关文章:

c++ - 当我们有设置值的 setter 时,为什么我们要使用参数化构造函数

c++ - 让应用程序在 Windows Phone 8.1 的锁屏下运行

c++ - 我不断收到错误消息,这是我第一次使用类,这是我的代码 :

c++ - 使用 c++11 的 <random> header ,获取 0 到 n 之间的整数的正确方法是什么?

c++ - 是否可以创建具有稍后类型定义的模板类?

c++ - 如何将 A<B<T>>* 解释为 A<C<T>>* 其中 B : public C<T>?

javascript - 在 AngularJS 中,如何使用指令创建具有更改变量名称的模板?

java - 从未确定的子类到父类(super class)的转换

java - 继承问题

c++ - 将空基类指针转换为子类指针?