初始化程序,但类型不完整
Initializer but incomplete type?
当我编译时,以下代码给了我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)
;它在CCD_ 3和CCD_。
它还说out_s
没有在Dictionary::translate (out_s,temp_eng_words[i])
;的这个范围内声明;。我不明白为什么一个参数被识别,而另一个却不被识别?
提前谢谢。
您必须包含头文件:
#include <sstream>
#include <string>
当您想要使用stringstream
和string
时。
同时:
Dictionary::translate (out_s,temp_eng_words[i]);
如果out_s
不是类的成员,那么在toElvish
内部使用out_s
之前,您似乎忘记了定义它。
同时:
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++;
}
您只需要包括sstream
如果使用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"。
相关文章:
- 模板元程序查找相似的连续类型名称
- 来自 Android 应用程序内部的 boost 类型的 boost::wrapexcept<boost::system::system_error> 的未捕获异常
- 类型限定宏帮助程序
- 在 C++20 中是否不再允许在 std 中对程序定义类型的函数模板进行专用化?
- 获取 OID(类型::b_oid)作为 MongoDB C++驱动程序中的字符串
- 如何在不破坏现有应用程序的情况下更改 API 中 stl 容器的数据类型?
- 为 Sql 服务器实现 odbc 包装器.将数据库数据读取为字符或要求驱动程序将数据转换为 C 类型
- 为什么字符串类型会导致 c++ 程序无法运行?
- 用于比较基元类型的std::可选的有趣程序集
- 连接到MS SQL的程序类型超出范围
- 程序无法编译:将 'int (*)[3][3]' 赋值为 'int [9][3][3]' 中的不兼容类型
- 在 C++ 中编写一个程序,要求用户输入 C++ 中所有基元类型的值
- 如何通过opencv中的程序参数定义特征类型?
- 添加类型名会导致程序编译失败
- 如何构建程序以避免查询对象类型?
- 使用MPI/OpenMP的C 程序带有派生数据类型(嵌套类对象)容器
- 制作需要接受字母和数字组合的密码类型程序
- 哪个会更好:CAD类型程序的列表,矢量或deques
- BOOLEAN数据类型程序代码
- 基本数据类型程序