我正在创建一个Trie来容纳小写字母的单词,并且我想重载[]运算符以将指针返回到包含下一个字符的节点
struct node{
node *next[27]; //26 pointers for each character from 'a'-'z' and 27th one for end of word.
node *& operator[] (char c){
if(ch>='a' && ch<='z') return *(next+(c-'a'));
return *(next+26); // *(next+26) equivalent to next[26] but since I'm overloading the operator []
}
};
我也尝试返回next+26
,但这会导致invalid initialization of non-const reference of type 'node*&' from an rvalue of type 'node**'
现在,当我检查main()
内的以下变量的类型时
node *trie=new node;
cout<<typeid(trie).name()<<" "
<<typeid(trie['_']).name()<<" "
<<typeid(trie['d']).name();
实际输出:P4node 4node 4node
所需的输出:P4node P4node P4node
从一天开始就弄乱了,请指出错误。