2013-01-14 103 views
1

我有一個問題,我最終下降到這個簡單的例子:調用基本方法模板子類

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) {} 
}; 

這將編譯於2010年MSVC罰款,但沒有相剋++ 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) 

發生了什麼事?

+0

http://www.parashift.com/c++-faq/nondependent-name-lookup-members.html – aschepler

回答

1

你要做的:

this->base_method(); 

Base<dimension>::base_method(); 

編譯器一般不會考慮在模板基類功能的解決方法。

+1

(請注意,第一個允許虛擬覆蓋,第二個不允許) – aschepler

+0

「編譯器通常不會考慮方法在模板化的基類中進行功能解析。「有趣。謝謝, – imallett

1

您必須明確地呼叫Base<dimensions>::base_method()