Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

c++ - Can I make a class template a friend of another class template?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...