错误:调用没有匹配函数

Error:No matching function for call to

本文关键字:函数 调用 错误      更新时间:2023-10-16

我对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
...