0

So I building a spell checker and I have this as my TrieNode class:

class TrieNode{
public:
    bool isWord;
    char word[100][20];
    TrieNode* letter[alphabetSize];

I have an insert method inside my Trie class which is:

    void insert(TrieNode* root, char* wordInsert);

I was able to insert the letters of char* word into my trie

This is what I have for my insert function:

void Trie::insert(TrieNode* root, char* wordInsert) {
    TrieNode* currentNode = root;
    int wordLength = strlen(wordInsert);
    for (int i = 0; i < wordLength; i++) {
        //strcpy(currentNode->word[i], currentNode->letter);
        int index = wordInsert[i]- 'a';
        if(!currentNode->letter[index]){
            currentNode->letter[index] = new TrieNode();
        }
        currentNode = currentNode->letter[index];
    }
    currentNode->isWord = true;

}

Now I want to insert the current->letter[i] into my other char* array in my TrieNode class called word

I tried doing

strcpy(currentNode->word, currentNode->letter[i])

but I get an error saying :

No matching function for call to 'strcpy'

How would I be able to get the elements from the letter array into the array called word which is also in my TrieNode class

3
  • No versions of strcpy that I know of take a TrieNode* as a parameter. You need a char *. Perhaps there's one inside the TrieNode. Commented Feb 26, 2020 at 4:54
  • Do you mean make the char word[100][20] into -> char* word[100][20] ? Commented Feb 26, 2020 at 4:57
  • Why tag your question with every c++ standard to day? You forgot c++20 anyways. Commented Feb 26, 2020 at 6:35

1 Answer 1

3

First of all why you use char * and strcpy if you tagged you question c++? Its better to use std::string to keep strings and probably std::vector or std::array for arrays. Also, you probably should consider using std::unique_ptr instead of plain pointers which you allocating with new.

Second, TrieNode is not string in itself. It has some strings inside, but you didn't specify which of these inside strings you want to copy.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.