']' 标记之前的预期主表达式"
expected primary-expression before ']' token"
本文关键字:表达式 更新时间:2023-10-16
//Purpose: To simulate the probability that a car will make a decision in a video game.
//40% of the time the car will turn left, 30% right, 20% straight, and 10% explode.
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <iomanip>
using namespace std;
void GetCount( count[] );
int main()
{
int count[4] = {0};
unsigned int k;
float theoretical;
srand( (unsigned) time(NULL) );
GetCount( int count[] );
cout.setf( ios::showpoint | ios::fixed );
cout << setprecision(2);
cout << " Car Simulation" << endl << endl
<< " Number of Experimental Theoretical % Error " << endl
<< " Times Selected Percent Percent " << endl
<< " ---------------- -------------- ------------- ------ " << endl;
for ( k = 0; k < 4; k++ )
{
switch(k)
{
case 0:
cout << "Left: ";
theoretical = 40;
break;
case 1:
cout << "Right: ";
theoretical = 30;
break;
case 2:
cout << "Straight: ";
theoretical = 20;
break;
default:
cout << "Explosion:";
theoretical = 10;
break;
} // end switch
cout << setw( 12 ) << count[ k ]
<< setw( 20 ) << count[ k ] / 10000.0 * 100.0
<< setw( 19 ) << theoretical
<< setw( 13 ) << ( ( count[ k ] - theoretical * 100 ) / (theoretical * 100) * 100 )
<< endl << endl;
} //end for
cout << endl << endl;
system("PAUSE");
return 0;
} //end main
void GetCount( int count[] )
{
int randNum;
unsigned int k;
for( k = 0; k < 10000; k++ )//generates random number 1-100 10,000 times
{
randNum = rand() % 100 + 1;
if( randNum <= 100 && randNum > 60 )
count[0]++;
else if( randNum <= 60 && randNum > 30 )
count[1]++;
else if( randNum <= 30 && randNum > 10 )
count[2]++;
else
count[3]++;
}//end for
}//end function definition
上面的代码是为了模拟汽车在视频游戏中做出决策。
在十字路口,汽车40
%的时间会左转,30%的时间会右转,20%的时间会直转,10%的时间汽车会爆炸。该程序执行其中的 10,000 个决策并输出结果。
除了函数"GetCount(("之外,一切都很好。调用此函数时,应该生成表示决策的随机数,并存储每个数字在其数组中生成的次数。
但是,当我编译程序时,我收到一个错误,说:
"in line 21, expected primary-expression before ']' token".
这与我调用函数的行相同。我已经尝试了一些方法来尝试修复它,但我不断收到一些错误。
任何建议将不胜感激,谢谢。
GetCount( int count[] );
应该是
GetCount(count);
该类型不必重复;编译器知道count
与您刚刚声明为int[]
变量相同。
相关文章:
- (C++)分析树以计算返回错误值的简单算术表达式
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 提升精神:解析布尔表达式并简化为规范范式
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 使用正则表达式regex_search在字符串中查找字符串
- 如何确认我的constexpr表达式实际上已经在编译时执行
- 概念中的cv限定符需要表达式参数列表
- 为什么constexpr的性能比正常表达式差
- 对于结构,表达式必须是可修改的ivalue
- 当一个值是非常量但用常量表达式初始化时使用constexpr
- 将fold表达式与std::一起用于两个元组
- 断言中的Fold表达式在某些计算机上编译,但在其他计算机上不编译
- 标记 '","' 之前的预期主表达式
- gcc和clang在表达式是否为常量求值的问题上存在分歧
- 如何计算具有指定类型的表达式的相对精度和绝对精度
- 带有用户定义类的c++折叠表达式
- 即使使用调试编译标志,表达式也是"optimized out"
- holeMenuProgram.cpp:38:1 错误:'}'令牌之前的预期主表达式
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- 这 4 个 lambda 表达式之间有什么区别?