So recently I am studying "Data structures algorithms and applications in C++". I encounter a code in which a class template is a friend of another class template. I am inexperienced with class templates.
template<class T>
class ChainNode{
friend Chain<T>
private:
T data;
ChainNode<T> *link;
};
template<class T>
class Chain {
public:
Chain() {first = 0;}
~Chain();
bool IsEmpty() const { return first == 0; }
int Length() const;
bool Find(int k, T& x) const;
int Search(const T& x) const;
Chain& Delete(int k, T& x);
Chain& Insert(int k, const T& x);
void Output(std::ostream &out) const;
private:
ChainNode<T> *first;
};
This is the original code form the book. I get the error:
chainList.hpp:7:9: error: 'Chain' does not name a type
7 | friend Chain;
If I change friend Chain<T>
to friend class Chain<T>
, I get the error:
chainList.hpp:7:15: error: 'Chain' is not a class template
7 | friend class Chain;
So my question is, how to make a class template a friend of another class template, or, is it impossible? Thanks!
question from:
https://stackoverflow.com/questions/65867754/can-i-make-a-class-template-a-friend-of-another-class-template 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…