C ++ Trie Dictionary - 迷失了两天

时间:2015-12-06 23:45:47

标签: c++ trie

这是我的dictionary.h文件:

#ifndef DICTIONARY_H
#define DICTIONARY_H

class Dictionary
{
    public:
        struct Node
        {
            std::string word;
            std::string definition;
            static const int ALPHABET_SIZE = 26;
            Node* next[ALPHABET_SIZE];
        };

        typedef Node TrieNode;
        typedef TrieNode* Trie;

        void createTrie();
        bool insertTrie(std::string word, std::string definition);
        bool loadDictionary(std::string fileName);
        bool lookup(std::string word);
        void deleteTrie();
        bool writeTrie(std::string fileName);
        Dictionary();
        ~Dictionary();
};

#endif // DICTIONARY_H

以下是我的dictionary.cpp文件:

#include <iostream>  
#include <string>
#include <fstream> 
#include <cctype>
#include "dictionary.h"
 using namespace std;

 Dictionary::Dictionary() 
{
    createTrie();
}

Dictionary::~Dictionary()
{
    deleteTrie();
}

void Dictionary::createTrie()
{
    static const int ALPHABET_SIZE = 26;
    Node *TrieNode = new Node;
    TrieNode->word = "";
    TrieNode->definition = "";
    for (int i = 0; i < ALPHABET_SIZE; i++)
    {
        TrieNode->next[i] = nullptr;
    }
}

bool Dictionary::insertTrie(string word, string definition)
{

    static const int ALPHABET_SIZE = 26;
    Node *newNode = nullptr;
    newNode = new Node;
    for (int i = 0; i < word.length(); i++)
    {  
    int letter = (int)word[i] - (int)'a';
        newNode->word[letter];
        newNode->definition;
        for (int i = 0; i < ALPHABET_SIZE; i++)
        {
            newNode->next[i] = nullptr;
        }
    }
} 

bool Dictionary::loadDictionary(string fileName)
{
    string word;
    string definition;
    fstream dataFile;
    dataFile.open(fileName);
    cout << "Loading dictionary..." << endl;
    if (dataFile)
    { 
        getline(dataFile, word, ':');
        getline(dataFile, definition);

        insertTrie(word, definition);
        while (!dataFile.eof())
        {
             getline(dataFile, word, ':');
             getline(dataFile, definition);

             insertTrie(word, definition);
        }
        dataFile.close();
        return 0;
    }    
    else 
    {
        return 1;
    }
}

bool Dictionary::lookup(string word)
{

    for (int i = 0; i < word.length(); i++)
    {  
    //char letter = (char)word[i] - (char)'a';
        Node* tmp = TrieNode->word[i];
    }
}

void Dictionary::deleteTrie()
{
    ;
}

bool Dictionary::writeTrie(string fileName)
{
    fstream dataFile;
    dataFile.open(fileName, fstream::out);
    dataFile.close();
    return 0;
}

这是我的main.cpp:

#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
#include "dictionary.h"
using namespace std;

int main()
{
    string fileName;
    string word;
    char answer = 'y';
    int loaded;
    int written;
    int lookedup;
    Dictionary dic;

    cout << "Please enter the name of the dictionary file: ";
    cin >> fileName;

    dic.createTrie();

    loaded = dic.loadDictionary(fileName);

    while (answer == 'y')
    {
      cout << "Would you like to look up a word? ";
      cin >> answer;

      if (answer == 'n')
      {
          cout << "Would you like to write the dictionary to a file? ";
          cin >> answer;
          if (answer == 'y')
          {
              cout << "Please enter the filename you wish to save it too: ";
              cin >> fileName;

              written = dic.writeTrie(fileName);
              return 0;
          }
      }
      else if (answer == 'y')
      {
          cout << "Enter a word: ";
          cin >> word;

          lookedup = dic.lookup(word);
      }
      else
          cout << "Please select a valid option.";
  }
  return 0;
}

现在,当我编译它时,我收到以下错误:

  

dictionary.cpp:在成员函数&bool Dictionary :: lookup(std :: __ cxx11 :: string&#39;:   dictionary.cpp:83:23:错误:在&#39; - &gt;&#39;之前预期的primary-expression;代币   节点* tmp = TrieNode-&gt; word [i];

真正令人烦恼的是,我不知道这个错误是否是我在这一点上做错的事情,或者如果我不断用指针挣扎意味着我搞砸了在链条的某个地方。

1 个答案:

答案 0 :(得分:3)

有助于指明行号,但是:Node* tmp = TrieNode->word[i];

TrieNode是一种类型,所以这没有意义。

更不用说createTrie()中有一个与Node *TrieNode = new Node;类型同名的局部变量 - 这是命名约定如何帮助避免混淆的完美示例。大多数人会告诉你“CapitalsForTypes,leadingLowerCaseForVars”(或some_variation_of_that)。