Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 非限定ID和模板类_C++_Templates_Iterator - Fatal编程技术网

C++ 非限定ID和模板类

C++ 非限定ID和模板类,c++,templates,iterator,C++,Templates,Iterator,我一直在为循环列表编写一个自定义类,称为CList。我一直在根据我很久以前完成的一个家庭作业,用大量的复制粘贴,所以我不完全记得我写的所有作品 无论如何,简单地尝试包含.h文件会导致using namespace std行出现错误: main.cpp:11: error: expected unqualified-id before ';' token 以及我的代码中指向函数的两个错误: In file included from main.cpp:9: CList.h:119: error:

我一直在为循环列表编写一个自定义类,称为CList。我一直在根据我很久以前完成的一个家庭作业,用大量的复制粘贴,所以我不完全记得我写的所有作品

无论如何,简单地尝试包含.h文件会导致using namespace std行出现错误:

main.cpp:11: error: expected unqualified-id before ';' token
以及我的代码中指向函数的两个错误:

In file included from main.cpp:9:
CList.h:119: error: non-template 'CIterator' used as template
CList.h:119: note: use 'CList<T>::template CIterator' to indicate that it is a template
在main.cpp:9中包含的文件中:
CList.h:119:错误:将非模板“CIterator”用作模板
CList.h:119:注意:使用'CList::template CIterator'表示它是一个模板
这就是所讨论的功能:

template <class T> 
typename CList<T>::CIterator<T> CList<T>::push(T const& v) 
{
    size++;  
    Node<T>* p = new Node<T>(v);

    if (this -> size_ == 1)
    {
        head = p;
        tail = p;
        p -> next = p;
        p -> prev = p;
    }
    else
    {
        tail -> next = p;
        p -> next = head;
        p -> prev = tail;
        head -> prev = p;
        tail = p;
    }

    return CIterator(p);
}
模板
类型名CList::CIterator CList::push(T const&v)
{
大小++;
节点*p=新节点(v);
如果(此->大小=1)
{
水头=p;
尾=p;
p->next=p;
p->prev=p;
}
其他的
{
尾部->下一步=p;
p->next=头部;
p->prev=尾部;
头->上一个=p;
尾=p;
}
返回系数(p);
}
我真的不明白这里的错误是什么。我告诉函数返回CList的CIterator,并指出此函数是CList类的一部分。这就是我读这句话时所理解的

typename CList<T>::CIterator<T> CList<T>::push(T const& v) 
typename CList::CIterator CList::push(T const&v)

为什么它认为CIterator是模板,而T显然是模板?我只是感到困惑。

错误消息指出,
CIterator
不被视为模板。这是因为它是一个依赖名称,编译器只能在实例化时确定它确实是一个嵌套模板。解决此问题的方法是告诉编译器它实际上是一个模板:

template <class T> 
typename CList<T>::template CIterator<T> CList<T>::push(T const& v) 
{
    ...
}
模板
typename CList::template CIterator CList::push(T const&v)
{
...
}
编译器不知道
CList::CIterator
是否是模板的原因是类
CList
可以专门用于某些类型
X
,以便在其中没有嵌套模板。但是,编译器只有在实例化模板时才能发现这些专门化