错误:调用没有匹配函数
Error:No matching function for call to
我对C++非常陌生,我正在尝试调用函数"jacobi",该函数为雅可比方法执行用户指定的迭代次数(或者至少我希望如此)。在我调用"jacobi"的行上,我收到错误"没有匹配函数来调用"jacobi"。我已经阅读了与此类似的其他帖子,并尝试将其应用于我自己的代码,但我没有成功。也许我的代码中还有其他问题导致此问题。如前所述,我C++很新,所以任何帮助将不胜感激,请为我分解。
#include <iostream>
using namespace std;
void jacobi (int size, int max, int B[size], int A[size][size], int init[size], int x[size]){
////
//// JACOBI
////
int i,j,k,sum[size];
k = 1;
while (k <= max) // Only continue to max number of iterations
{
for (i = 0; i < size; i++)
{
sum[i] = B[i];
for (j = 0; j < size; j++)
{
if (i != j)
{
sum[i] = sum[i] - A[i][j] * init[j]; // summation
}
}
}
for (i = 0; i < size; i++) ////HERE LIES THE DIFFERENCE BETWEEN Guass-Seidel and Jacobi
{
x[i] = sum[i]/A[i][i]; // divide summation by a[i][i]
init[i] = x[i]; //use new_x(k+1) as init_x(k) for next iteration
}
k++;
}
cout << "Jacobi Approximation to "<<k-1<<" iterations is: n";
for(i=0;i<size;i++)
{
cout <<x[i]<< "n"; // print found approximation.
}
cout << "n";
return;
}
int main (){
// User INPUT
// n: number of equations and unknowns
int n;
cout << "Enter the number of equations: n";
cin >> n;
// Nmax: max number of iterations
int Nmax;
cout << "Enter max number of interations: n";
cin >> Nmax;
// int tol;
// cout << "Enter the tolerance level: " ;
// cin >> tol;
// b[n] and a[n][n]: array of coefficients of 'A' and array of int 'b'
int b[n];
int i,j;
cout << "Enter 'b' of Ax = b, separated by a space: n";
for (i = 0; i < n; i++)
{
cin >> b[i];
}
// user enters coefficients and builds matrix
int a[n][n];
int init_x[n],new_x[n];
cout << "Enter matrix coefficients or 'A' of Ax = b, by row and separate by a space: n";
for (i = 0; i < n; i++)
{
init_x[i] = 0;
new_x[i] = 0;
for (j = 0; j < n; j++)
{
cin >> a[i][j];
}
}
jacobi (n, Nmax, b, a, init_x, new_x);
}
问题:
有几个问题与数组的使用有关:
- 不能按值将数组作为参数传递。
- 如果维度是可变的,则不能将多维数组作为参数传递
- 不能在 C++ 中定义可变长度的数组
当然,有一些方法可以做所有这些事情,但它使用不同的原则(动态分配,使用指针)并且需要额外的工作(特别是对于多维数组元素的访问)。
幸运的是,还有一个更简单的解决方案!
解决方案:
对于这种代码,您应该选择vector
:这些管理可变长度并且可以按值传递。
对于jacobi()
函数,您所要做的就是更改其定义:
void jacobi(int size, int max, vector<int> B, vector<vector<int>> A, vector<int> init, vector<int> x) {
int i, j, k;
vector<int> sum(size); // vector of 'size' empty elements
// The rest of the function will work unchanged
...
}
但是请注意:向量可以是可变大小的,并且此雅可比奥实现假定所有向量都具有预期的大小。 在专业级代码中,您应该检查是否如此。
对于 main() 的实现,代码几乎没有变化。 您所要做的就是用向量定义替换数组定义:
...
vector<int> b(n); // creates a vector that is initialized with n elements.
...
vector<vector<int>> a(n,vector<int>(n)); // same idea for 2 dimensional vector (i.e. a vector of vectors)
vector<int> init_x(n), new_x(n); // same principle as for b
...
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 使用共享指针的函数调用,其对象应为 const
- C++:编译时检查匹配的函数调用对?
- 函数调用C++中的参数太少
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 返回指向对象的指针的函数调用是否为 prvalue?
- C++ 如何重载 [] 运算符并进行函数调用
- 代码的效率. 转到和函数调用
- 是同一作用域的函数部分中的函数调用
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 类型擦除的std::function与虚拟函数调用的开销