0

我收到错误:

']' 标记之前的预期主表达式

在这条线上:

berakna_histogram_abs(histogram[], textRad);

有人知道为什么吗?

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

4 回答 4

3

在 main() 函数调用是错误的:

berakna_histogram_abs(histogram[], textRad);

应该:

berakna_histogram_abs(histogram, textRad);

您只需要[]在函数声明中而不是在调用函数时。

于 2013-08-24T15:50:30.847 回答
3

您对该函数的调用berakna_histogram_abs是错误的main(),它应该是:

berakna_histogram_abs(histogram, textRad);
//                             ^

函数声明中的[]are 表示它需要一个数组,您不必将它用于函数调用。

您还有另一个错误:

该函数的原型berakna_histogram_abs是:

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

在你main()定义和之前

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

同样在您的主要内容中,您尝试将字符串作为参数传递,因此您的代码应该是:

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

int main()
{
    // ...
}

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

最后一件事:尝试传递参考或const参考而不是价值:

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

您的最终代码应如下所示:

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;
      }
   } 
}
于 2013-08-24T15:52:33.403 回答
2

您将表格传递给功能错误。你应该简单地:

berakna_histogram_abs(histogram, textRad);

更重要的是,您首先声明:

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

但比你试图定义的:

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

这就是您的编译器认为第二个参数是int而不是string. 函数原型应与声明一致。

于 2013-08-24T15:52:19.100 回答
0

错误在于仅传递histogram[]
传递 在参数中,您已将第二个参数定义为,但在定义函数时,您将第二个参数保留为类型 更改初始定义histogram
intstring

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

void berakna_histogram_abs(int histogram[], string textRad);
于 2013-08-24T15:52:46.133 回答