2017-03-07 305 views
1
#ifndef TRIEAPI 
#define TRIEAPI 

#include <vector> 
#include <string> 
#include <unordered_map> 

using namespace std; 

typedef struct NodeStruct 
{ 
    bool validNgram = false; 
    unordered_map<string, struct NodeStruct> children; 
    struct NodeStruct* parent; 
    string nodeWord; 

    NodeStruct(struct NodeStruct* par, string w) : parent(par), nodeWord(w) {} //constructor (yeah, structs have them too) 
    NodeStruct() {} 
}Node; 


void addNgramTrie(Node* root, string ngram); 
void findNgramsTrie(Node* root, string query); 
void splitText(string query, vector<string> &words); 
void deleteNgramTrie(Node* root, string ngram); 
void recursiveParentDeletion(Node* node, Node *root); 


#endif // TRIEAPI 

當我嘗試編譯程序我得到關於所述pair.h該頭文件中的錯誤(以g ++編譯5.4 C++ 11):C++:pair.h編譯器錯誤 - 對具有不完全的類型

trie.h:14:46: required from here 
/usr/include/c++/5/bits/stl_pair.h:102:11: error: ‘std::pair<_T1, _T2>::second’ has incomplete type 
     _T2 second;    /// @c second is a copy of the second object 
     ^
In file included from trie.cpp:10:0: 
trie.h:11:16: note: forward declaration of ‘struct NodeStruct’ 
typedef struct NodeStruct 

我不明白我錯在哪裏。

+0

*「當我嘗試編譯程序時」* - 什麼程序?我沒有看到一個程序。 – IInspectable

+0

@iinspectable,你看到了什麼?錯誤? :-P – WhiZTiM

+0

@JimS,那些'typedef'在C++中是沒用的,只是給代碼添加了混亂。您不需要「Eloborated類型說明符」 – WhiZTiM

回答

0
typedef struct NodeStruct 
{ 
    bool validNgram = false; 
    unordered_map<string, struct NodeStruct> children; 

這不可能工作。想象一下,例如,如果unordered_map的大小取決於它包含的類型的大小。這當然是可能的。因此,要知道unordered_map<string, struct NodeStruct>的大小,首先需要知道struct NodeStruct的大小。但是由於struct NodeStruct包含這樣一個unordered_map,您還需要知道地圖的大小以瞭解結構的大小。

這怎麼可能工作?也許在地圖上使用unique_ptr<struct NodeStruct>

+0

但是,爲什麼當我使用地圖而不是無序的地圖時,我沒有得到這個錯誤? – JimS

+0

@JimS語言級別的回答:你很幸運,它不需要使用'std :: map'。 –

+0

@JimS當你違反規則時,規則就會被破壞。遵守規則,系統也會遵守。 (我的觀點表明它不能在一般情況下工作,並不是沒有可能發生的具體情況,我的觀點表明你不應該期待它的工作。) –

相關問題