我如何获得一个没有错误的朋友类模板函数

问题描述 投票:1回答:1

对于一项作业,我需要能够创建一个模板化的朋友类,其余的程序都可以正常工作,但是我无法越过错误。我提供了用于定义这两个类的代码以及这些错误的照片。如果我需要更具体的问题,请告诉我,但老实说,我对模板一无所知。谢谢。

    template<class T>
class Node{
        friend class NodeStack;
        public:
                Node();
                Node(const T & data, Node<T> * next = NULL);
                T & data();
                const T & data() const;

        public:
                Node<T> * m_next;
                T m_data;
};



template<class T>
class NodeStack{

template<class U>
friend std::ostream & operator<< (std::ostream & os, const NodeStack<T> & nodeStack);

public:
        NodeStack();
        NodeStack(size_t count, const T & value = T() );
        NodeStack(const NodeStack<T> &other);
        ~NodeStack();

        NodeStack<T> & operator=(const NodeStack<T> & rhs);

        T & top();
        const T & top() const;

        void push(const T & value);
        void pop();

        size_t size() const;
        bool empty() const;
        bool full() const;
        void clear();
        void serialize(std::ostream & os) const;

private:
        Node<T>* m_top;
};
c++ class templates friend
1个回答
0
投票

将您的朋友声明更改为

friend class NodeStack<T>; 

我应该做的把戏。

© www.soinside.com 2019 - 2024. All rights reserved.