Daniel o Keeffe
Daniel o Keeffe

Reputation: 578

Expected primary expression before

I have the following code in a translator.h file

class Dictionary
{

 public:
  Dictionary(const char dictFileName[]);
  void translate(char out_s[], const char s[]); 

and I call the function in my translator.cpp file as follows

for (int i=0; i<2000;i++) 
{
Dictionary:: translate (out_s[],temp_eng_words[i]); 
}

Which gives me an error "expected primary expression before ']' token". I don't understand what is wrong, and decided against putting up the whole code if the problem can be found in the above snippet.

Any ideas??

I already tried it without the [] for out_s, but it gives me an error "cannot call member function void dictionary::translate (char*, const char *) without object". I'll post the whole code to give a clearer indication of what the problem might be.

Translator.cpp

#include <iostream>
#include <fstream>
#include <sstream>
#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;
int k=0;
char temp_eng_words[2000][50];
char out_s;
//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
{

strcpy (temp_eng_words[k],word.c_str());

k++;

}

for (int i=0; i<2000;i++)
{
Dictionary:: translate (out_s,temp_eng_words[i]); // ERROR RELATES TO THIS LINE -     cannot call member function like this.    error -  expected primary expression
// before ] if written out_s[].

}
}



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

}
}

translator.h

const int MAX_NUM_WORDS=2000;
const int MAX_WORD_LEN=50;

class Dictionary
{

public:
Dictionary(const char dictFileName[]);
void translate(char out_s[], const char s[]); // s represents a wor out_s, the    translated word

private:

char englishWord[MAX_NUM_WORDS][MAX_WORD_LEN];
char elvishWord[MAX_NUM_WORDS][MAX_WORD_LEN];
int numEntries;
};

class Translator
{
public:
 Translator(const char s[]);
 char toElvish(const char out_s[],const char s[]);
 char toEnglish(char out_s[], const char s[]);

 private:
  Dictionary dict;

};

Upvotes: 0

Views: 4174

Answers (4)

jhole
jhole

Reputation: 4236

Try

for (int i=0; i<2000;i++) 
{
  Dictionary:: translate (out_s,temp_eng_words[i]); 
}

Upvotes: 1

masoud
masoud

Reputation: 56539

The problem is passing out_s[], try this one:

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

if out_s is an array then passing it without [] is enough.

Upvotes: 2

Joseph Mansfield
Joseph Mansfield

Reputation: 110768

First note that a function signature like void translate(char out_s[], const char s[]) is actually equivalent to void translate(char* out_s, const char* s).

One of the arguments you're trying to pass is out_s[] - this isn't valid. You either need to pass the array itself out_s (which will undergo array-to-pointer conversion) or a specific element of it out_s[i].

From where I'm standing, it seems that you want to pass out_s.

Upvotes: 1

nullptr
nullptr

Reputation: 11058

Compiler expects an array index in your actual parameter out_s[].

What do you mean by out_s[]? If out_s is an array of char or a pointer to char, then pass out_s (without brackets).

Upvotes: 1

Related Questions