C++ C++;函数返回值未赋值

C++ C++;函数返回值未赋值,c++,function,return,assign,C++,Function,Return,Assign,我遇到了一只奇怪的虫子 我正在返回一个指针,在返回之前,我验证了指针是否有效&是否有内存 但是,在函数作用域之后,当我尝试在main()中使用返回值时,它变为NULL。 我还尝试返回指针的解引用值,它在返回之前是一个修改的结构,在main()中是一个未修改的结构 这应该像一本字典 #include <iostream> #include <fstream> #include <string> #include "trie.h" using namespace

我遇到了一只奇怪的虫子

我正在返回一个指针,在返回之前,我验证了指针是否有效&是否有内存 但是,在函数作用域之后,当我尝试在main()中使用返回值时,它变为NULL。 我还尝试返回指针的解引用值,它在返回之前是一个修改的结构,在main()中是一个未修改的结构

这应该像一本字典

#include <iostream>
#include <fstream>
#include <string>
#include "trie.h"

using namespace std;

int alphaLoc(char segment){
    return (int)segment - 97;
}

//inserts a word in the tree
void insert(TrieNode &node, const std::string &word){
    int locationInAlphabet = alphaLoc(word[0]);
    if (node.letters[locationInAlphabet] == NULL){
        node.letters[locationInAlphabet] = new TrieNode;
    }
    if (word.length() == 1){
        if (node.letters[locationInAlphabet]->isWord == true){
            cout<<"Word Already Exsit"<<endl;
        }
        node.letters[locationInAlphabet]->isWord = true;
    }
    else{
        insert(*(node.letters[locationInAlphabet]), word.substr(1,word.length()-1));
    }
}

//returns the node that represents the end of the word
TrieNode* getNode(const TrieNode &node, const std::string &word){
    int locationInAlphabet = alphaLoc(word[0]);
    if (node.letters[locationInAlphabet] == NULL){
        return NULL;
    }
    else{
        if (word.length() == 1){
            return (node.letters[locationInAlphabet]);
        }
        else{
            getNode(*(node.letters[locationInAlphabet]), word.substr(1,word.length()-1));
        } 
    }
}

int main(){
    TrieNode testTrie;
    insert(testTrie, "abc");
    cout<< testTrie.letters[0]->letters[1]->letters[2]->isWord<<endl;
    cout<<"testing output"<<endl; 
    cout<< getNode(testTrie, "abc")->isWord << endl;
    return 1;
}
trie.h:

#include <string>

struct TrieNode {
    enum { Apostrophe = 26, NumChars = 27 };
    bool isWord;
    TrieNode *letters[NumChars];
    TrieNode() {
        isWord = false;
         for ( int i = 0; i < NumChars; i += 1 ) {
             letters[i] = NULL;
         } // for
    }
}; // TrieNode

void insert( TrieNode &node, const std::string &word );

void remove( TrieNode &node, const std::string &word );

std::string find( const TrieNode &node, const std::string &word );
#包括
结构三节点{
枚举{撇号=26,NumChars=27};
布尔语;
三元组*字母[NumChars];
三节点(){
isWord=false;
对于(int i=0;i
您在
getNode(*(node…
)之前有
return
缺失


如果这一行在某个时刻执行,在执行控制流到达
getNode
函数的末尾之后,并且没有
return
语句在这里。它将导致未定义的返回值,这总是不好的。您必须始终从函数中返回一些明确的值。

get之前缺少
return
节点(*(node…
?@Riateche-将此作为答案,我将删除我的-您的评论在我回答之前进入。这是递归语句..不..它应该是递归的,直到单词的最后一个字符这里肯定是个错误。如果这一行在某个时刻执行,在此执行之后,控制流r选择
getNode
函数的结尾,此处没有
return
语句。这将导致未定义的返回值,返回值总是不正确的。您必须始终从函数返回明确的值。
#include <string>

struct TrieNode {
    enum { Apostrophe = 26, NumChars = 27 };
    bool isWord;
    TrieNode *letters[NumChars];
    TrieNode() {
        isWord = false;
         for ( int i = 0; i < NumChars; i += 1 ) {
             letters[i] = NULL;
         } // for
    }
}; // TrieNode

void insert( TrieNode &node, const std::string &word );

void remove( TrieNode &node, const std::string &word );

std::string find( const TrieNode &node, const std::string &word );