2

I am working on a translation/transliteration program, that reads an english story, and translates it to elvish, using an english/elvish dictionary. After the code shown below, i explain the error that i receive.

I have a lot of code, I am not sure if i should post it all, but I will post what I think should be sufficient. Apologies if my code seems bizarre - but I am only a beginner.

There is a main file, a header file with two Classes: Translator and Dictionary, and a cpp file to implement the class functions.

I have a constructor that reads in the dictionary file to dictFileName and copys the english words into englishWord, and the elvish words into elvishWord:

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();

}

In the main file, the english lines are read into the toElvish function, and tokenized into an array of words, temp_eng_words.

Within this toElvish function, I am calling another function; translate, which reads in temp_eng_words and is supposed to return the elvish words:

char Translator::toElvish(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
    }

}

This is the translate function:

char Dictionary::translate (char out_s[], const char s[])
{

 int i;

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

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

My problem is that when I run the program, I get the error '*out_s was not declared in this scope*'.

If you have read all of this, thanks; any suggestions/clues would be much appreciated. :)

  • You don't get that when you run it, you get that when you try to compile it. And you get it because `out_s` isn't declared in `toElvish`. I haven't managed to find out what you want to pass there. Probably some sufficiently large empty buffer. – Daniel Fischer May 08 '13 at 20:25

1 Answers1

0

As you can see in your code below, you have used out_s in your function, but you have not declared it in your function. You can use global variables or local variables in your function. I suggest you read this

char Translator::toElvish(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
}}