【问题标题】:Expected primary expression before之前预期的主要表达式
【发布时间】:2013-05-18 12:56:02
【问题描述】:

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

class Dictionary
{

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

我在我的 translate.cpp 文件中调用函数如下

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

这给了我一个错误“']'标记之前的预期主表达式”。我不明白哪里出了问题,如果问题可以在上面的 sn-p 中找到,我决定不放整个代码。

有什么想法吗??

我已经尝试过不带 [] 的 out_s,但它给了我一个错误“无法在没有对象的情况下调用成员函数 void dictionary::translate (char*, const char *)”。我将发布整个代码,以便更清楚地说明问题可能是什么。

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;

};

【问题讨论】:

  • 关于您的第二个错误:translateDictionary 的非静态成员。你必须在一个对象上调用它,Dictionary 的一个实例。 Translator 类中似乎有一个,所以您的意思可能是 dict.translate(out_s, temp_eng_words[i]);

标签: c++ syntax-error


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    编译器需要在您的实际参数out_s[] 中有一个数组索引。

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

    【讨论】:

      【解决方案3】:

      首先请注意,像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

      【讨论】:

        【解决方案4】:

        试试

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-07-26
          • 1970-01-01
          • 2018-11-27
          • 2020-01-16
          相关资源
          最近更新 更多