c++ - 嵌套类的不完整类型

标签 c++

我有以下形式的代码:

class Trie{
public:
    Trie( ) : root( new TrieNode( ) ){ };

    // Inserts a word into the trie.
    void insert( std::string word );

    // Returns if the word is in the trie.
    bool search( std::string word );

    // Returns if there is any word in the trie
    // that starts with the given prefix.
    bool startsWith( std::string prefix );

private:
    class TrieNode;
    std::unique_ptr<TrieNode> root;
};

class Trie::TrieNode{
public:
    TrieNode( ) : eow( false ){ };

    TrieNode* appendChar( char tar );

    void end( );

    bool isEnd( );

    TrieNode* getChar( char tar );

    int getInd( char tar );

private:
    std::array<std::unique_ptr<TrieNode>, 26> data;
    bool eow;                        // End of word
};

但是,在第三行 Trie(): root( new TrieNode() ) 中,编译器继续提示 TrieNode 不完整。我该如何解决?

谢谢!

最佳答案

TrieNode定义之后定义Trie的构造函数

class Trie::TrieNode{...}

// must be AFTTER the class Trie is fully defined
Trie::Trie( ) : root( new TrieNode( ) ){ };

否则 Trie 的构造函数需要 TrieNode 的完整定义,因为它需要构造一个新对象,因此会出现错误。

关于c++ - 嵌套类的不完整类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35050373/

相关文章:

c++ - C++是否被视为Von Neumann编程语言?

C++:如何在两个不同的重载函数中使用具有两个不同名称的相同数据类型?

c++ - 绘制 sf::Text 时出现问题。 C++

c++ - 无法获得方法模板以正确专门化

android - 在另一个源文件中调用 JNI C 函数

c++ - 为创建的 hijackWindow() 上下文

c++ - 构造函数设置?

c++ - 将大文件加载到内存中并在程序的所有运行时间中保留它是否错误?

c++ - 使用相同的方法将一个变量用于不同的类

c++ - 调用 EnterCriticalSection 时出错