调用基本方法模板子类

时间:2013-01-14 15:54:35

标签: c++ templates

我遇到了一个问题,我最终简化为这个简单的例子:

template <int dimensions> class Base {
    protected:
        Base(void) {}
    public:
        virtual ~Base(void) {}

        void base_method(void) {}
};
template <int dimensions> class MyClass : public Base<dimensions> {
    public:
        MyClass(void) : Base<dimensions>() {
            base_method();
        }
        ~MyClass(void) {}
};

这在MSVC 2010上编译得很好,但是在g ++ 4.6中失败了:

main2.cpp: In constructor âMyClass<dimensions>::MyClass()â:
main2.cpp:12:16: error: there are no arguments to âbase_methodâ that depend on a template parameter, so a declaration of âbase_methodâ must be available [-fpermissive]
main2.cpp:12:16: note: (if you use â-fpermissiveâ, G++ will accept your code, but allowing the use of an undeclared name is deprecated)

发生了什么事?

2 个答案:

答案 0 :(得分:1)

您必须明确调用Base<dimensions>::base_method()

答案 1 :(得分:1)

你必须这样做:

this->base_method();

Base<dimension>::base_method();

编译器通常不会考虑模板化基类中的方法来进行函数解析。

相关问题