gpt4 book ai didi

c++ - primary-expression before ']'错误含义

转载 作者:行者123 更新时间:2023-11-30 00:36:20 25 4
gpt4 key购买 nike

我正在尝试修复有错误的代码,但我还有一个我无法弄清楚的错误。错误提示:']' 标记之前的预期主表达式,这是什么意思?我检查了放错地方的分号和变量名,但找不到任何东西。这是我的代码(我已经注释了错误的行):

// countOnes.cpp
#include<iostream>
#include<cstdlib>
using namespace std;

void countOnes( int array[] ); // Count the number of 1s in a given int array.
const int arraySize = 10;
int array[ arraySize ];
int countOne = 0;

int main()
{
for ( int i = 0; i <= arraySize; i++ )
{
array[ i ] = rand() % 3; // Fill array with 0, 1 or 2 randomly
cout << array[ i ] << " ";
}
countOnes( array[], arraySize ); //ERROR

cout << "\n The number of 1s in the array: " << countOne;

return 0;
}

void countOnes( int array[], int arraySize )
{
for ( int i = 0; i <= arraySize; i++ )
if ( array[ i ] == 1 )
countOne = countOne + 1;
return;
}

最佳答案

不需要方括号。

countOnes(array<strike>[]</strike>, arraySize);

关于c++ - primary-expression before ']'错误含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16110490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com