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

标签 c++ templates generics

我正在尝试基于二叉树搜索 实现一个集合。所以我从 root(指向 Node 的指针)开始构建这个集合,其中 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
}

所以我遇到了这个错误:

/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 {
 ^

我看过很多与此错误相关的帖子,但其中大部分是由于在函数定义之前编写函数实现引起的。如您所见,fatherOfNode 的实现在其定义之下,因此我的情况似乎并非如此。

知道发生了什么事吗?

最佳答案

NodeSet 中的一个内部类,所以在这个类之外你需要解决这个问题:

Set<T>::Node 

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

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

Here it is, working.

关于c++ - 对象没有命名类型 - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39781362/

相关文章:

c++ - 错误 : invalid use of incomplete type (Maybe a definition issue)

c# - 扩展方法和通用约束的问题

java - Java 中的类型推断(在 C# 中)

c++ - 无需规则即可创建目标 .moc

c# - C++ 和 C# 之间的并排依赖关系

c++ - 为什么我的程序不能在GDB在线编译器/调试器或Visual Studio C++ 2019中运行?

c++ - 模板化类构造函数在结构中使用错误的重载

c++ - 执行时间比较

javascript - ng-repeat 和 ng-class 的 Angular 模板指令

java - 无法解析类型变量 'T'(使用 com.yaml.Example 类的上下文)