Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializer but incomplete type?

Tags:

c++

The following code gives me 2 errors when i compile

#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();

   }
} 

The first one is at std::string istringstream stm(str); where it says it the variable has an initializer but incomplete type. If I put in std::string istringstream stm(str); it says expected initializer before stm andstm was not declared in the scope.

It also says out_s was not declared in this scope at Dictionary::translate (out_s,temp_eng_words[i]);. I don't see why one parameter is recognisied and one is not?

Thanks in advance.

like image 799
Daniel o Keeffe Avatar asked Dec 16 '22 10:12

Daniel o Keeffe


1 Answers

You have to include header file:

#include <sstream>
#include <string>

when you want to use stringstream and string.

Meanwhile:

Dictionary::translate (out_s,temp_eng_words[i]);

If out_s is not a member of the class, you seems forgot to define out_s before using it inside toElvish.

Meanwhile:

 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++;
 }
like image 63
taocp Avatar answered Jan 02 '23 10:01

taocp