在“.”之前需要“类型名”,因为“.”是一个依赖范围
need ‘typename’ before ‘..’ because ‘..’ is a dependent scope
尝试
编译模板类时遇到问题。
在 .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>
^
我做错了什么?
没有
称为dataType
的成员,我假设返回类型应该只是模板dataType
:
template <typename dataType>
dataType Node<dataType>::getData() const {
return nodeData;
}
在这种情况下,编译器消息具有误导性,因为它找不到正确的定义,它假定dataType
引用模板参数。
template <typename DataType>
class Node {
public:
using dataType = DataType;
private:
dataType nodeData;
Node<dataType>* nextLink;
Node<dataType>* previousLink;
public:
Node(const dataType& nodeData);
dataType getData() const;
};
template <typename DataType>
typename Node<DataType>::dataType Node<DataType>::getData() const {
return nodeData;
};
像这样的typename
。
http://melpon.org/wandbox/permlink/Agu2s6vw6OLfbbRh
所呈现的示例代码不完整,因此必须猜测具体问题。
但是,以下是以实用方式完成该类的方法,而不会遇到像您遇到的问题:
template< class Item >
struct Node
{
Node* next;
Node* prev;
Item item;
};
表明有时可以在不知道确切细节的情况下解决问题。
相关文章:
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何设置一个范围来提取我想要获得的信息
- 不计算一个范围内的完美数
- 我有一个数组,我想输入一个范围,然后找到范围内所有偶数的总和?
- 在 C++20 中将多个范围适配器连接到一个范围中
- 如何将一个范围替换为矢量中的另一个范围?
- 将整数范围映射到另一个范围
- 是否存在一个范围::视图::group_by对应项,它将所有元素都考虑在内,而不是只考虑连续的元素
- 我已经编写了C++代码将 boost::optional 视为一个范围,但它无法编译
- 当在另一个范围中,将SelfAdjointeigensolver保存为成员的结果被重新引入
- C :在一个范围内计数偶数 /奇数
- 删除重复的元素并给出一个范围总和
- 为什么即将到来的范围库不支持一个范围的容器初始化
- 将无符号整数加法/减法括在一个范围周围
- 更快的方法将数字从范围转换为另一个范围
- 在一个范围内的阵列中找到三胞胎
- 如何在C++中选择多维数组中的一个范围
- 如何检查一个范围内的值是否是另一个范围内的值的倍数
- 将 std::vector<int> 设置为一个范围
- 正在检查排序列表中是否有一个范围内的数字