error: expected primary-expression before ']' token

我只是一个虾纸丫 提交于 2021-02-11 15:42:40

问题


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;
      }
   } 
}

回答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. Prototype of function should be consistent with declaration.




回答2:


In main() call of function is wrong:

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.




回答3:


Your call to the function berakna_histogram_abs is wrong in main(), it should be :

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 :

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

before you main() definition and

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 :

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;
      }
   } 
}



回答4:


error is in passing histogram[]
pass histogram only
In parameter you have defined second argument to be int but while defining the function you have kept second argument as string type
change initial definition

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

to

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


来源:https://stackoverflow.com/questions/18420334/error-expected-primary-expression-before-token

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!