【问题标题】:error: expected primary-expression before ']' token错误:“]”标记之前的预期主表达式
【发布时间】:2013-08-24 15:49:17
【问题描述】:

我收到了错误:

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

在这一行:

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

【问题讨论】:

  • 这不是将表传递给函数的正确方法。

标签: c++ arrays


【解决方案1】:

在main()函数调用错误:

berakna_histogram_abs(histogram[], textRad);

应该是:

berakna_histogram_abs(histogram, textRad);

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

【讨论】:

  • 改成那个会给我这个错误:不能将参数'2'的'std :: string {aka std :: basic_string }'转换为'int'到'void berakna_histogram_abs(int * , int)'|
  • @user1207319 berakna_histogram_abs声明的第二个参数
【解决方案2】:

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

berakna_histogram_abs(histogram, textRad);
//                             ^

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

你还有另一个错误:

函数berakna_histogram_abs的原型是:

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

在你之前main()定义和

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

在你的 main 中,你试图传递一个字符串作为参数,所以你的代码应该是:

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

【讨论】:

    【解决方案3】:

    您将表传递给函数错误。你应该简单地:

    berakna_histogram_abs(histogram, textRad);
    

    还有什么你先声明的:

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

    但比你试图定义的:

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

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

    【讨论】:

      【解决方案4】:

      错误在传递histogram[]
      仅通过histogram
      在参数中,您将第二个参数定义为 int,但在定义函数时,您将第二个参数保留为 string 类型
      改变初始定义

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

      到

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

      【讨论】:

        猜你喜欢
        • 2013-01-13
        • 2017-09-24
        • 2012-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多