4

次のコードをコンパイルすると、2 つのエラーが発生します。

#include <iostream>
#include <fstream>
#include <cstring>
#include "Translator.h"

using namespace std;

void Dictionary::translate(char out_s[], const char s[])
{
    int i;
    char englishWord[MAX_NUM_WORDS][MAX_WORD_LEN];

    for (i=0;i < numEntries; i++)
    {
       if (strcmp(englishWord[i], s)==0)
           break;
    }

    if (i<numEntries)
       strcpy(out_s,elvishWord[i]);
}

char Translator::toElvish(const char elvish_line[],const char english_line[])
{
   int j=0;

    char temp_eng_words[2000][50];
    //char temp_elv_words[2000][50]; NOT SURE IF I NEED THIS

    std::string str = english_line;
    std:: istringstream stm(str);
    string word;
    while( stm >> word) // read white-space delimited tokens one by one
    {
        int k=0;
        strcpy (temp_eng_words[k],word.c_str());
        k++;
     }

     for (int i=0; i<2000;i++) // ERROR: out_s was not declared in this scope
     {
       Dictionary::translate (out_s,temp_eng_words[i]); // ERROR RELATES TO THIS LINE
      }
}

Translator::Translator(const char dictFileName[]) : dict(dictFileName)
{
    char englishWord[2000][50];
    char temp_eng_word[50];
    char temp_elv_word[50];
    char elvishWord[2000][50];
    int num_entries;

    fstream str;

    str.open(dictFileName, ios::in);
    int i;

    while (!str.fail())
    {
      for (i=0; i< 2000; i++)
      {
         str>> temp_eng_word;
         str>> temp_elv_word;
         strcpy(englishWord[i],temp_eng_word);
         strcpy(elvishWord[i],temp_elv_word);
      }

      num_entries = i;
 }

    str.close();

   }
} 

最初のものはstd::string istringstream stm(str); 変数には初期化子がありますが、型が不完全です。私が入れたらstd::string istringstream stm(str); stmと の前に期待されるイニシャライザが表示されますstm was not declared in the scope

out_sまた、;でこのスコープで宣言されていない とも言いDictionary::translate (out_s,temp_eng_words[i])ます。1 つのパラメーターが認識され、1 つが認識されない理由がわかりません。

前もって感謝します。

4

3 に答える 3

1

を使用すると、トランスレータははるかに簡単になりますstd::map

#include <map>
#include <string>

// map[english word] returns the elvish word.
typedef std::map<std::string, std::string> Dictionary;  

// Define the dictionary
Dictionary english_to_elvish_dictionary;


std::string To_Elvish(const std::string& english_word)
{
    Dictionary::iterator    iter;
    std::string             elvish_word;
    iter = english_to_elvish_dictionary.find(english_word);
    if (iter != english_to_elvish_dictionary.end())
    {
        // English word is in dictionary, return the elvish equivalent.
        elvish_word = *iter;
    }
    return elvish_word;
}

上記のコード フラグメントは、ほとんどのコードを置き換え、C 文字列の配列の配列に関する問題を軽減します。コードが少ない == 問題が少ない。

発生している問題のリストを表示するには、StackOverflow で「[c++] elvish english」を検索してください。

于 2013-05-14T14:45:11.213 に答える