bryo
bryo

Reputation: 39

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

Upvotes: 0

Views: 2196

Answers (4)

Himanshu Pandey
Himanshu Pandey

Reputation: 736

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

Upvotes: 0

Pierre Fourgeaud
Pierre Fourgeaud

Reputation: 14510

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

Upvotes: 3

Qiu
Qiu

Reputation: 5751

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.

Upvotes: 2

Grijesh Chauhan
Grijesh Chauhan

Reputation: 58261

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.

Upvotes: 3

Related Questions