简体   繁体   English

当Base和Derived都使用Derived类型参数进行模板化时,调用Base构造函数时发生编译器错误

[英]Compiler Error When Calling Base Constructor when both Base and Derived are Templated With Derived Type Parameter

I am struggling to understand why the following code does not compile: 我正在努力理解为什么以下代码无法编译:

template <class T>
class Base {
    public:
        Base(int a){}
};
template <class T>
class Derived: public Base<T>  {
    public:
        Derived(int a): Base(a){}
};
int main(){}

On my compiler (gcc 5.4.0 with C++ 11) this outputs the error message 在我的编译器(带有C ++ 11的gcc 5.4.0)上,这将输出错误消息

error: class 'Derived<T>' does not have any field named 'Base'
         Derived(int a): Base(a){}

I see that this is somewhat similar to Template base constructor call in member initialization list error , though that test case actually compiles for me while this one does not: The main difference seems to be that both Base and Derived use the same type parameter. 我看到这有点类似于成员初始化列表错误中的Template base构造函数调用 ,尽管该测试用例实际上是为我编译的,而这个却没有:主要区别似乎是BaseDerived使用相同的类型参数。 Additionally, it compiles fine if I explicitly add the type parameters or I give an explicit scope for base, as in 另外,如果我显式添加类型参数或为base提供显式范围,则编译效果很好,例如

template <class T>
class Base {
    public:
        Base(int a){}
};

template <class T>
class Derived: public Base<T>  {
    public:
        Derived(int a): Derived::Base(a){}
};

int main(){}

What's going on? 这是怎么回事? Am I misunderstanding when injected class names may be used? 当使用注入的类名时,我是否会误解?

The injected class name Base is a member of the class Base , and since the base class is dependent, its scope is not searched during unqualified name lookup. 注入的类名Base是类的一个成员Base ,并且由于基类是相关的,其范围并不限定名称查找期间搜索。 As such, using the name Base will only find the class template, not the injected class name of Base<T> . 因此,使用名称Base只会找到类模板,而不是注入的Base<T>类名。 That is why you must write Base<T> . 这就是为什么必须编写Base<T>

Derived::Base works because it causes name lookup to be postponed until Derived is instantiated. Derived::Base之所以起作用,是因为它导致名称查找被推迟到实例化Derived之前。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “模板多态”在调用 function 用于基类型的模板化参数时,具有派生类型? - “template polymorphism” when calling function for templated parameter of base type, with derived type? 从派生类对象调用基类方法时出错。 基类和派生类都是模板化的 - Error calling a base class method from a derived class object. Both base and derived classes are templated 为什么在调用基类复制构造函数(C ++)时将派生类类型作为参数传递? - Why passing derived class type as parameter when calling base class copy constructor (C++)? 模板化派生类时访问基本成员数据错误 - Accessing base member data error when derived class is templated 调用基本方法和派生方法 - calling both base and derived methods C ++ Base构造函数使用将在派生构造函数中构造的参数进行调用 - C++ Base constructor calling with parameter that will be constructed in the derived constructor 在派生指针上显式调用基类析构函数时发生编译错误 - Compile error when explicitly calling the base class destructor on a derived pointer 调用仅在 base 中实现但未派生的 function 时如何警告(或错误)? - How to warn (or error) when calling a function that is only implemented in base but not derived? 在派生类的构造函数的主体中调用基本构造函数 - Calling base constructor in body of derived class' constructor 派生类构造函数调用基类构造函数 - Derived class constructor calling base class constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM