【发布时间】:2013-05-14 14:22:30
【问题描述】:
以下代码在我编译时出现 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]); 的这个范围内声明。我不明白为什么一个参数被识别而一个不被识别?
提前致谢。
【问题讨论】:
-
在您的
while (stm >> word)循环中,您将k设置为零,因此每次迭代都会覆盖相同的第一个temp_eng_words[0]。尝试用笔和纸执行。
标签: c++