2012-11-13 167 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是一个从属名称,其含义必须被消除歧义。