在模板类中声明模板friend类中的编译器错误 [英] Compiler error in declaring template friend class within a template class
问题描述
我一直试图实现我自己的链表类教学目的。
我在迭代器声明中指定了List类作为朋友,似乎正在编译。
这些是我使用的3个类的接口:
strong> Node.h:
#define null(Node< T& b $ b template< class T>
class Node {
public:
T content;
Node< T> * next;
Node< T> * prev;
Node(const T& _content):
content(_content),
next(null),
prev(null)
{}
};
Iterator.h:
#includeNode.h
template< class T>
class Iterator {
private:
Node< T> * current;
迭代器(Node< T> *);
public:
bool isDone()const;
bool hasNext()const;
bool hasPrevious()const;
void stepForward();
void stepBackwards();
T& currentElement()const;
friend class List< T> ;;
};
List.h
#include< stdexcept>
#includeIterator.h
template< class T>
class List {
private:
Node< T> * head;
Node< T> * tail;
unsigned int items;
public:
List();
List(const List< T>&);
List& operator =(const List< T&;);
〜List();
bool isEmpty()const {
return items == 0;
}
unsigned int length()const {
return items;
}
void clear();
void add(const T&);
T remove(const T&)throw(std :: length_error& std :: invalid_argument&);
迭代器< T> createStartIterator()const throw(std :: length_error&);
迭代器< T> createEndIterator()const throw(std :: length_error&);
};
这是我一直试图运行的测试程序:
trial.cpp
using namespace std;
#include< iostream>
#includeList / List.cc
int main()
{
List< int> myList;
for(int i = 1; i <= 10; i ++){
myList.add(i);
}
for(Iterator< int> it = myList.createStartIterator();!it.isDone(); it.stepForward()){
cout< it.currentElement()<< endl;
}
return 0;
}
当我尝试编译它时,编译器会给我以下错误: / p>
Iterator.h:26:错误:'列表'不是模板
Iterator.h:Iterator实例化:
trial.cpp:18:
Iterator.h:12:错误:'struct List'所需的模板参数
List.cc:在成员函数'Iterator List :: createStartIterator()const [with T = int]':
strong> trial.cpp:18:从此处实例化
Iterator.h:14:error:'Iterator :: Iterator [with T = int]'is private
List.cc:120:error:在此上下文中
似乎它不能识别朋友声明。
我在哪里错了?
尝试添加前瞻性声明
模板< class T>类List; Iterator.h
开头的
< - 这可能是您需要允许 Iterator
类中的 friend
声明工作。
I have been trying to implement my own linked list class for didactic purposes.
I specified the "List" class as friend inside the Iterator declaration, but it doesn't seem to compile.
These are the interfaces of the 3 classes I've used:
Node.h:
#define null (Node<T> *) 0
template <class T>
class Node {
public:
T content;
Node<T>* next;
Node<T>* prev;
Node (const T& _content) :
content(_content),
next(null),
prev(null)
{}
};
Iterator.h:
#include "Node.h"
template <class T>
class Iterator {
private:
Node<T>* current;
Iterator (Node<T> *);
public:
bool isDone () const;
bool hasNext () const;
bool hasPrevious () const;
void stepForward ();
void stepBackwards ();
T& currentElement () const;
friend class List<T>;
};
List.h
#include <stdexcept>
#include "Iterator.h"
template <class T>
class List {
private:
Node<T>* head;
Node<T>* tail;
unsigned int items;
public:
List ();
List (const List<T>&);
List& operator = (const List<T>&);
~List ();
bool isEmpty () const {
return items == 0;
}
unsigned int length () const {
return items;
}
void clear ();
void add (const T&);
T remove (const T&) throw (std::length_error&, std::invalid_argument&);
Iterator<T> createStartIterator () const throw (std::length_error&);
Iterator<T> createEndIterator () const throw (std::length_error&);
};
And this is the test program I've been trying to run:
trial.cpp
using namespace std;
#include <iostream>
#include "List/List.cc"
int main ()
{
List<int> myList;
for (int i = 1; i <= 10; i++) {
myList.add(i);
}
for (Iterator<int> it = myList.createStartIterator(); !it.isDone(); it.stepForward()) {
cout << it.currentElement() << endl;
}
return 0;
}
When I try to compile it, the compiler gives me the following errors:
Iterator.h:26: error: ‘List’ is not a template
Iterator.h: In instantiation of ‘Iterator’:
trial.cpp:18: instantiated from here
Iterator.h:12: error: template argument required for ‘struct List’
List.cc: In member function ‘Iterator List::createStartIterator() const [with T = int]’:
trial.cpp:18: instantiated from here
Iterator.h:14: error: ‘Iterator::Iterator(Node*) [with T = int]’ is private
List.cc:120: error: within this context
Seems like it is not recognizing the friend declaration. Where did I go wrong?
try adding a forward declaration
template <class T> class List;
at the start of Iterator.h
-- that might be what you need to allow the friend
declaration inside the Iterator
class to work.
这篇关于在模板类中声明模板friend类中的编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!