gpt4 book ai didi

c++ - 翻译/音译问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:57:21 25 4
gpt4 key购买 nike

我正在开发一个翻译/音译程序,该程序读取一个英文故事,然后使用英文/ Sprite 语词典将其翻译成 Sprite 语。在下面显示的代码之后,我解释了我收到的错误。

我有很多代码,我不确定是否应该全部发布,但我会发布我认为应该足够的代码。如果我的代码看起来很奇怪,我深表歉意 - 但我只是一个初学者。

有一个ma​​in文件,一个包含两个类的header文件:TranslatorDictionary,以及一个cpp文件来实现类的功能。

我有一个构造函数,可以将字典文件读入dictFileName并将英文单词复制到englishWord,将 Sprite 词复制到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 函数,并标记为单词数组 temp_eng_words

在这个 toElvish 函数中,我正在调用另一个函数;翻译,它读入 temp_eng_words 并且应该返回 Sprite 词:

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 未在此范围内声明*”。

如果您已经阅读了所有这些内容,谢谢;任何建议/线索将不胜感激。 :)

最佳答案

正如您在下面的代码中看到的,您在函数中使用了 out_s,但尚未在函数中声明它。您可以在函数中使用全局变量或局部变量。我建议你阅读 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
}}

关于c++ - 翻译/音译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16449506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com