0

エラーが発生します:

']' token` の前に一次式が必要です

この行で:

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

関数への呼び出し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){...}
//                                    ^^^^^^

また、メインでは文字列を引数として渡そうとしているため、コードは次のようになります。

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これは、コンパイラが 2 番目の引数がではなく. であると考える方法stringです。関数のプロトタイプは、宣言と一致している必要があります。

于 2013-08-24T15:52:19.100 に答える
0

histogram[]
パスhistogramのみを渡す際にエラーが発生しました
パラメーターでは、2番目の引数をint定義しましたが、関数を定義している間、2番目の引数をstring
変更の初期定義として保持しました

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

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