访问模板化基类的模板化方法

Accessing templated methods of a templated base class

本文关键字:方法 基类 访问      更新时间:2023-10-16

可能的重复项:
在"模板化基类"中调用模板方法时出错

以下代码使用 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 甚至在知道BaseQNativeWiindow是什么类型之前就假设findChildren不是模板。谁能解释一下?

谢谢。

你必须说:

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

由于findChildren是一个从属名称,因此必须消除其含义的歧义。