【发布时间】:2015-10-08 18:13:19
【问题描述】:
我编写了以下代码来用随机浮点数填充长度为len(已经初始化)的double数组:
void FillRay(double (&array)[] , const unsigned int len, const double a , const double b)
{
for(unsigned int i = 0 ; i < len ; ++i )
{
array[i] = randFloat(a,b); // Fill array at i with random number
}
return;
}
但是,当我在 main() 中使用我的 FillRay 函数时(见 main 结尾)......
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <math.h>
using namespace std;
int main(){
double a;
double b;
cout << "Please enter lower bound (a): ";
cin >> a;
cout << endl;
cout << "Please enter upper bound (b): ";
cin >> b;
cout << endl;
//Calculate mean and variance
double mu = (b - a)/2;
double sigma = pow(b - a , 2)/12;
//Declare arrays (with langths)
unsigned int shorter = 1000;
unsigned int longer = 100000;
double shortArray[shorter];
double longArray[longer];
// Fill array of length 1K
FillRay(shortArray , shorter, a , b); // ***THIS IS MY PROBLEM AREA***
return 0;
}
...我收到错误 No matching function for call to 'FillRay'
有人可以解释我做错了什么吗?谢谢!
【问题讨论】:
-
可变长度数组不是标准的 C++,尽管您的编译器可能支持它作为扩展。使用后果自负。
-
函数
FillRay在哪里定义的?
标签: c++ arrays pass-by-reference void