【发布时间】:2017-02-15 23:34:09
【问题描述】:
在尝试编译模板类时遇到问题。
在.h文件中
template <typename dataType>
class Node {
private:
dataType nodeData;
Node<dataType>* nextLink;
Node<dataType>* previousLink;
public:
Node(const dataType& nodeData);
// methods
在.template文件中
template <typename dataType>
Node<dataType>::dataType Node<dataType>::getData() const {
return nodeData;
};
我在尝试编译时遇到的错误是:
need ‘typename’ before ‘Node<dataType>::dataType’ because ‘Node<dataType>’ is a dependent scope
Node<dataType>::dataType Node<dataType>::getData() const {
然后我添加类型名,然后它给了我这个错误:
error: expected nested-name-specifier before ‘dataType’
typename dataType getData() const;
^
error: expected ‘;’ at end of member declaration
error: declaration of ‘int Node<dataType>::dataType’
error: shadows template parm ‘class dataType’
template <typename dataType>
^
我做错了什么?
【问题讨论】:
-
看起来您将
typename放在dataType之前,而不是在Node<dataType>::dataType之前,如编译器所示。是这样吗?代码修改后能显示源码吗? -
消息说将
typename放在‘Node<dataType>::dataType之前。不早于dataType -
@BrianCain 不要认为它与那个重复
-
看起来您已将
typename放在类的声明中,而不是实现中。作为缺乏可重复的例子投票结束。 -
也就是说,对于
Node,具有 getter 成员函数的访问控制不会在此级别为您购买任何东西。没有优势,但冗长和复杂。我只是删除它,使用简单的struct。
标签: c++