【问题标题】:Accessing templated methods of a templated base class [duplicate]访问模板化基类的模板化方法[重复]
【发布时间】:2012-11-13 16:28:28
【问题描述】:

可能重复:
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 甚至在知道 BaseQNativeWiindow 是什么类型之前就假定 findChildren 不是模板。谁能解释一下?

谢谢。

【问题讨论】:

  • 看起来应该可以了...试试BaseQNativeWindow::template findChildren&lt; QWidget* &gt;()是否解决了它
  • this post 可能会有所帮助。
  • 确实添加“模板”解决了它,谢谢。需要复习一下我的模板知识。如果您创建它,我会接受您的回答:)。

标签: c++ qt templates gcc4


【解决方案1】:

你不得不说:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    相关资源
    最近更新 更多