首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >初始值设定项但类型不完整?

初始值设定项但类型不完整?
EN

Stack Overflow用户
提问于 2013-05-14 22:22:30
回答 3查看 5.6K关注 0票数 4

下面的代码在我编译时给出了两个错误

代码语言:javascript
复制
#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),它会在stmstm was not declared in the scope之前显示预期的初始化器。

它还表示,在% Dictionary::translate (out_s,temp_eng_words[i]);上未在此范围内声明out_s。我不明白为什么一个参数被识别而另一个参数不被识别?

提前谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-05-14 22:24:17

你必须包含头文件:

代码语言:javascript
复制
#include <sstream>
#include <string>

当您想要使用stringstreamstring时。

同时:

代码语言:javascript
复制
Dictionary::translate (out_s,temp_eng_words[i]);

如果out_s不是该类的成员,那么在toElvish中使用out_s之前,您似乎忘记了定义它。

同时:

代码语言:javascript
复制
 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++;
 }
票数 10
EN

Stack Overflow用户

发布于 2013-05-14 22:24:32

您只需要包含sstream

票数 1
EN

Stack Overflow用户

发布于 2013-05-14 22:45:11

如果你使用std::map,你的翻译器会简单得多。

代码语言:javascript
复制
#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“。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16545753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档