在 c++ 中传递数组的 through 函数,在 ']' 之前得到错误:primary-expression
Passing array's through functions in c++, getting error:primary-expression before ']'
我正在上基础csci,计算机编程课程,并且已经花了几个小时摆弄这些代码。我试图通过一个函数传递数组,我的代码将无法编译。我不知道我的代码出了什么问题。内容如下:
int buildArray (double*);
int main ()
{
int valuesPerLine;
int randomValues;
double array[110];
srand(time(NULL));
cout<<"How many values should be displayed per line? ";
cin>>valuesPerLine;
randomValues=buildArray(array[]);
cout<<array[50];
return (0);
}
int buildArray (double array[])
{
int t; //t is the total number of numbers in the array
t=rand();
array[t];
for (int i=0; i<t; i++)
{
array[i]=randDouble();
}
return(t);
}
cout<<array[50];
只是为了我自己看看答案是否改变。它不会出现在最终代码中。
有什么简单的我错过了吗?我通常能够在课堂上帮助其他人编写代码,但由于某种原因,我不能弄清楚这个。
谢谢你做的一切!注:这不是完整的代码,我知道在这种情况下,我没有说randDouble是什么,但我不认为这很重要,因为代码似乎编译得很好。如果需要,请告诉我,我可以贴在下面。
将数组传递给函数时,只传递指向数组第一个元素的指针。
在你的例子中,它应该是:
randomValues = buildArray(array);
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- 如何修复此错误"expected primary expression before 'int'"?
- 野牛的错误:“ yyval.yystype :: expr”没有类型
- 如何修复"Expected Primary-expression before ')' token"错误?
- 简单继承实现中的构建错误"expected primary-expression before 'float' "来源
- C++错误"tabulate.cc:19: error: expected primary-expression before ',' token"
- 将指针传递给函数时出现"expected primary-expression before"错误
- C++ 结构和"expected primary-expression"错误
- 在 c++ 中传递数组的 through 函数,在 ']' 之前得到错误:primary-expression
- 使用模板方法"expected primary expression"错误
- 如何修复"expected primary-expression"错误
- 我的主函数中有一些 "expected primary-expression before 'int' " 类型的错误。怎么了?
- C++ "expected primary-expression before ‘(’ token"错误
- 从VS2012到gcc(cygwin)时"Expected primary-expression"编译错误
- systemC 错误:没有构造函数"BlackBox<R、expR、C、expC、T、expT> ...等