VOID 函数通过引用传递数组,但在使用时,我会得到"No matching matching function for call to..."
VOID function passes array by reference, but when used, I get "No matching matching function for call to..."
我写了下面的代码来用随机浮点数填充长度为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;
}
…我得到错误没有匹配的函数调用'FillRay'
有人能解释一下我做错了什么吗?谢谢!将函数原型修改为:
void FillRay(double* array, const unsigned int len, const double a , const double b)
此外,使用size_t而不是int表示数组大小也是一个很好的做法。
void FillRay(double (&array)[] , const unsigned int len, const double a , const double b)
double (&array)[]
尝试在不指定数组大小的情况下通过引用获取数组。这是不可能的。您还指定了一个len
参数,这在技术上是无关紧要的。如果你想使用没有任何专有扩展的标准c++,那么你有三个选择:
- 完全改变程序逻辑,使数组大小在编译时是固定的。
- 让函数取一个
double*
,并让从数组到指针的自动转换(非正式地称为"衰减")发生。 - 使用
std::vector<double>
.
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- "no matching function for call to 'Vector::Vector'"错误
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- 为什么当我在 std::string 中打开文件名的 fstream 时出现"no matching function"错误?
- 从 TVector2 类派生复杂类:"No matching function for call to Complex::Complex()"
- 学习 c++,编译错误"no matching function for call"
- C++ "error: no matching function"
- 尝试实例化模板类的对象时出现"No Matching Constructor"错误
- 结构错误:"no matching function for call to "
- 创建函数变体向量时"No matching function for call"错误
- 为什么"std::isdigit"在 Windows 上报告C++"no matching overloaded function"
- 编译时二叉搜索树错误的反向迭代器表示"no matching function call for operator=()"
- 如果子类中没有构造函数方法,则错误"no matching function for call to 'LGame::LGame(String&)'"
- 提升多精度浮点数128:标准::exp错误:'no matching function for call'
- "error: no matching function for call to",我无法更改结构的定义
- 替换派生对象向量中的对象"no matching function to call"
- 我的程序显示许多错误,"error: no matching function for call to 'getPercentScore'"
- 为什么即使我定义了它们"no matching overload found"也会出现错误
- 为什么有一个指向此代码行(第 7 行)的"no matching call for function"错误?