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]);。我不明白为什么一个参数被识别而一个不被识别?

提前致谢。

4

3 回答 3

10

您必须包含头文件:

#include <sstream>
#include <string>

当你想使用stringstreamand时string

同时:

Dictionary::translate (out_s,temp_eng_words[i]);

如果out_s不是该类的成员,您似乎忘记out_s在内部使用它之前进行定义toElvish

同时:

 while( stm >> word) // read white-space delimited tokens one by one
 {
    int k=0; //^^Why do you initialize k everytime you read a word?
    strcpy (temp_eng_words[k],word.c_str());
    k++;
 }
于 2013-05-14T14:24:16.670 回答
1

你只需要包括 sstream

于 2013-05-14T14:24:31.783 回答
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 回答