2012-11-13 83 views
1

可能重複:
Error calling template method in 「templated-base-class」訪問模板化基類的模板的方法

下面的代碼編譯與MSVC10但不是用gcc 4.2.1:

template<class BaseQNativeWindow> 
class NativeWindow : public BaseQNativeWindow 
{ 
public: 
    NativeWindow(AIPanelPlatformWindow handle) : BaseQNativeWindow(handle) 
    {} 

protected: 
    virtual void closeEvent(QCloseEvent *e) 
    { 
    QList<QWidget *> childrenList; 
    childrenList = BaseQNativeWindow::findChildren< QWidget * >(); // GCC ERROR 
    foreach(QWidget *child, childrenList) 
    { 
     child->close(); 
    } 
    } 
}; 

這是gcc抱怨的:

error: expected primary-expression before ‘*’ token 
error: expected primary-expression before ‘>’ token 
error: expected primary-expression before ‘)’ token 

findChildrenBaseQNativeWindow必須提供的模板化方法。 gcc似乎認爲findChildren甚至在知道BaseQNativeWiindow是什麼類型之前不是模板。任何人都可以解釋嗎?

謝謝。

+5

看起來它應該工作...嘗試如果'BaseQNativeWindow :: template findChildren < QWidget* >()'解決它 –

+1

[this post](http://stackoverflow.com/questions/610245/where-and-why-do -i-have-to-put-the-template-and-typename-keywords/613132#613132)可能會有所幫助。 – juanchopanza

+0

確實添加「模板」解決它,謝謝。需要刷新我的模板知識。如果你創建它,我會接受你的答案:)。 – Badder

回答

5

你不得不說:

BaseQNativeWindow::template findChildren< QWidget * >() 
//     ^^^^^^^^ 

由於findChildren是一個從屬名稱,其含義必須被消除歧義。