简体   繁体   English

错误:“]”标记之前的预期主表达式

[英]error: expected primary-expression before ']' token

I'm getting the error: 我收到错误消息:

expected primary-expression before ']' token` ']'标记前的预期主要表达式`

On this line: 在这行上:

berakna_histogram_abs(histogram[], textRad);

Anybody know why? 有人知道为什么吗?

const int ANTAL_BOKSTAVER = 26;  //A-Z

void berakna_histogram_abs(int histogram[], int antal);

int main() {

   string textRad = "";
   int histogram[ANTAL_BOKSTAVER];

   getline(cin, textRad);

   berakna_histogram_abs(histogram[], textRad);
   return 0;
}

void berakna_histogram_abs(int tal[], string textRad){

   int antal = textRad.length();

   for(int i = 0; i < antal; i++){

      if(textRad.at(i) == 'a' || textRad.at(i) == 'A'){
        tal[0] + 1;
      }
   } 
}

In main() call of function is wrong: 在main()中,函数调用错误:

berakna_histogram_abs(histogram[], textRad);

should be: 应该:

berakna_histogram_abs(histogram, textRad);

You need [] in function declaration only but not at the time of call function. 仅在函数声明中需要[] ,而在调用函数时则不需要。

Your call to the function berakna_histogram_abs is wrong in main() , it should be : 您在main()对函数berakna_histogram_abs调用是错误的,应该是:

berakna_histogram_abs(histogram, textRad);
//                             ^

The [] are in the function declaration to indicated that it takes an array, you don't have to use it for the function call. 函数声明中的[]表示它接受一个数组,您不必在函数调用中使用它。

You have another error : 您还有另一个错误:

The prototype of the function berakna_histogram_abs is : 函数berakna_histogram_abs的原型为:

void berakna_histogram_abs(int histogram[], int antal);
//                                          ^^^

before you main() definition and main()定义之前和

void berakna_histogram_abs(int tal[], string textRad){...}
//                                    ^^^^^^

Also in your main you are trying to pass a string as argument, so your code should be : 同样在您的主目录中,您尝试将字符串作为参数传递,因此您的代码应为:

void berakna_histogram_abs(int histogram[], string antal);

int main()
{
    // ...
}

void berakna_histogram_abs(int tal[], string textRad){
    //....
}

And last thing : try to pass reference or const reference instead of value : 最后一件事:尝试传递引用或const引用而不是值:

void berakna_histogram_abs(int tal[], string& textRad)
//                                          ^

You final code should look like : 您的最终代码应如下所示:

const int ANTAL_BOKSTAVER = 26;  //A-Z

void berakna_histogram_abs(int histogram[], const string& antal);

int main() {

   string textRad = "";
   int histogram[ANTAL_BOKSTAVER];

   getline(cin, textRad);

   berakna_histogram_abs(histogram, textRad);
   return 0;
}

void berakna_histogram_abs(int tal[], const string& textRad) {

   int antal = textRad.length();

   for(int i = 0; i < antal; i++){

      if(textRad.at(i) == 'a' || textRad.at(i) == 'A'){
        tal[0] + 1;
      }
   } 
}

You are passing table to function wrong. 您正在传递表以使功能出错。 You should simply: 您应该简单地:

berakna_histogram_abs(histogram, textRad);

What's more you firstly declare: 此外,您首先要声明:

void berakna_histogram_abs(int histogram[], int antal);

But than you're trying to define: 但是比您要定义的要多:

void berakna_histogram_abs(int tal[], string textRad){}

That's way your compiler think that second argument is int and not a string . 这样,您的编译器就会认为第二个参数是int而不是string Prototype of function should be consistent with declaration. 函数的原型应与声明一致。

error is in passing histogram[] 错误在于传递histogram[]
pass histogram only 仅通过histogram
In parameter you have defined second argument to be int but while defining the function you have kept second argument as string type 在参数中,您已将第二个参数定义为int但是在定义函数时,您将第二个参数保留为string类型
change initial definition 更改初始定义

void berakna_histogram_abs(int histogram[], int antal);

to

void berakna_histogram_abs(int histogram[], string textRad);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM