对象没有命名类型 - C++

jsc*_*man 5 c++ generics templates

我正在尝试实现基于二叉树搜索集合.所以我从一个(指向节点的指针)开始构建这个集合,其中Node有一个值,右边和左边的子节点(两个指向Node的指针).所以这样我可以通过将root-> right指向创建的节点来设置根节点右侧的新节点,依此类推.看看定义:

template <class T>
class Set
{
    public:
        Set();
        ~Set();
        void push(const T&);
        bool belongs(const T&) const;
        void remove(const T&);
        const T& min() const;
        const T& max() const;
        unsigned int cardinal() const;
        void show(std::ostream&) const;

        friend ostream& operator<<(ostream& os, const Set<T> &c) {
            c.show(os);
            return os;
        }

    private:

        struct Node
        {
            Node(const T& v);
            T value;
            Node* left;
            Node* right; 
        };

        Node* root_;
        int cardinal_;

    Node & fatherOfNode(const Node & root, const T & key, const bool hook) const;

};

...

// This function is the one with errors.
template <class T>
Node & Set<T>::fatherOfNode(const Node & root, const T & key, const bool hook) const {
    // Some code
}
Run Code Online (Sandbox Code Playgroud)

所以我有这个错误:

/home/jscherman/ClionProjects/algo2-t3-bts/set.hpp:247:1: error: ‘Node’ does not name a type
 Node & Set<T>::fatherOfNode(const Node & root, const T & key, const bool hook) const {
 ^
Run Code Online (Sandbox Code Playgroud)

我见过很多与此错误相关的帖子,但大多数都是在定义之前编写函数实现引起的.正如您所看到的,fatherOfNode的实现低于其定义,因此它似乎不是我的情况.

关于发生了什么的任何想法?

Fan*_*Fox 4

Node是 中的内部类Set,因此在此类之外,您需要通过以下方式解决此问题:

Set<T>::Node 
Run Code Online (Sandbox Code Playgroud)

所以你的函数定义需要是:

template <class T>
typename Set<T>::Node & Set<T>::fatherOfNode(const Set<T>::Node & root, const T & key, const bool hook) const {
Run Code Online (Sandbox Code Playgroud)

就在这里,正在工作。