我正在做一个翻译/音译程序,它读一个英语故事,然后用英语/精灵词典把它翻译成精灵。在下面显示的代码之后,我将解释我收到的错误。
我有很多代码,我不确定是否应该全部发布,但是我会发布我认为应该足够的内容。很抱歉,如果我的代码看起来很奇怪--但我只是个初学者。
有一个主文件,一个包含两个类的头文件:Translator和Dictionary,以及一个cpp文件来实现类函数。
我有一个构造函数,它将字典文件读入dictFileName,并将英文单词复制到englishWord,中,将elvish单词复制到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();
}在主文件中,英文行被读取到toElvish函数中,并被标记为一个单词数组,名为toElvish。
在这个temp_eng_words函数中,我调用了另一个函数: toElvish,它用toElvish读取并返回精灵词:
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
}
}这是转换函数:
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]);
}我的问题是,当我运行程序时,我会得到错误'*out_s没有在这个范围内声明*‘。
如果您已经阅读了所有这些,谢谢;任何建议/线索都将不胜感激。:)
发布于 2019-07-21 10:12:28
正如您在下面的代码中所看到的,您在函数中使用了out_s,但是您没有在函数中声明它。可以在函数中使用全局变量或局部变量。我建议你读这
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
}}https://stackoverflow.com/questions/16449506
复制相似问题