没有匹配函数调用已定义的函数

时间:2013-07-06 10:08:50

标签: c++ pointers

由于某种原因,我在第15行得到错误“没有匹配函数调用'OptimalBinarySearchTree'”。我不确定它是否与我传递数组指针的方式有关或者我是否遗漏了某些东西。我之前从未尝试过传递2D数组,所以它可能会弄乱它。

#include <iostream>

using namespace std;

void OptimalBinarySearchTree(int n, int *P[n], int (*values)[n][n], int (*roots)[n][n]);    

int main()
{
    const int n = 18;
    char A[n] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R'};
    int P[n] = {995,22,23,562,33,8,60,118,30,723,807,626,15,89,21,128,626,621};
    int values[n][n];
    int roots[n][n];

    OptimalBinarySearchTree(n, P, values, roots);

    return 0;
}

void OptimalBinarySearchTree(int n, int *P[n], int (*values)[n][n], int (*roots)[n][n])
{
    for (int i = 1; i <= (n+1); i++)
    {
        (*values)[i][i] = 0;
    }
    for (int i = 1; i <= n; i++)
    {
        (*values)[i][i] = *P[i];
        (*roots)[i][i] = i;
    }

    for (int d = 1; d <= (n-1); d++)
    {
        for (int i = 1; i <= (n-d); i++)
        {
            int j = i + d;
            int sumP = 0;
            int minValue = 999999999;
            int minRoot = 0;

            for (int k = i; k <= j; k++)
            {
                sumP += *P[k];
                int value = (*values)[i][k-1] + (*values)[k+1][j];
                if (value < minValue)
                {
                    minValue = value;
                    minRoot = k;
                }
            }

            (*values)[i][j] = sumP + minValue;
            (*roots)[i][j] = minRoot;
        }
    }
};

任何帮助将不胜感激。谢谢,

1 个答案:

答案 0 :(得分:0)

您正在添加一个间接层:

void OptimalBinarySearchTree(int n, int *P[n], int (*values)[n][n], int (*roots)[n][n]);    

int main()
{
    const int n = 18;
    char A[n] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R'};
    int P[n] = {995,22,23,562,33,8,60,118,30,723,807,626,15,89,21,128,626,621};
    int values[n][n];
    int roots[n][n];

    OptimalBinarySearchTree(n, P, values, roots);

    return 0;
}

int *P[n]int (*values)[n][n]等意味着您的函数采用int指针数组(P)和int指针的二维数组(称为{{ 1}})。但是,您传递的是values值数组和int值的二维数组。

删除int,它会变得更好。但是,我非常确定它是完全非法的,或者是传递*的编译器扩展,然后将其用于int n数组的维度。由于它是C ++,您可能需要考虑使用valuesvector<int>