之前的预期主要表达

时间:2013-05-18 12:56:03

标签: c++ syntax-error

我在translator.h文件中有以下代码

class Dictionary
{

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

我在translator.cpp文件中调用该函数,如下所示

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

这给了我一个错误“''之前预期的主要表达''令牌”。我不明白什么是错的,如果问题可以在上面的片段中找到,我决定不提出整个代码。

任何想法??

我已经尝试过没有[] for out_s,但它给了我一个错误“无法调用成员函数void dictionary :: translate(char *,const char *)without object”。我将发布整个代码,以更清楚地指出问题可能是什么。

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;

};

4 个答案:

答案 0 :(得分:2)

问题是通过out_s[],试试这个:

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

如果out_s是一个数组,那么在没有[]的情况下传递它就足够了。

答案 1 :(得分:1)

编译器需要实际参数out_s[]中的数组索引。

out_s[]你是什么意思?如果out_schar的数组或指向char的指针,则传递out_s(不带括号)。

答案 2 :(得分:1)

首先请注意,void translate(char out_s[], const char s[])等函数签名实际上等同于void translate(char* out_s, const char* s)

您尝试传递的其中一个参数是out_s[] - 这是无效的。您需要传递数组本身out_s(将进行数组到指针转换)或其特定元素out_s[i]

从我站立的地方来看,您似乎想要通过out_s

答案 3 :(得分:1)

尝试

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