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

Abe*_*ant 4 c++ inheritance templates const-iterator c++11

出于某种原因,以下代码给出了错误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.
    { }
};
Run Code Online (Sandbox Code Playgroud)

奇怪的是,当我删除::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.
    { }
};
Run Code Online (Sandbox Code Playgroud)

另外,以下函数也没有给出错误,因此它似乎与模板基类调用与const_iterator的组合有关:

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

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

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

Ada*_*dam 6

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

TemplateBase(typename std::map<std::string, T>::const_iterator anIterator)
{ }
Run Code Online (Sandbox Code Playgroud)

  • @Aberrant因为你没有在第二种情况下引用嵌套类型.另外,你觉得[这](/sf/ask/42717181/?rq=1 )是你的问题的副本?我的近距离投票是有约束力的,所以在我这样做之前我想和你核实一下. (2认同)