c ++“无匹配函数错误”返回2D数组

时间:2014-09-18 15:53:51

标签: c++ function pointers

我是c ++的新手,并且相当肯定错误在我传入函数的变量中。本质上,我在double_arrays.cpp文件中定义了两个函数 - 首先是确定两个向量之间的欧几里德距离(作为具有10个值的数组传入,此函数效果很好)。另一个(nearestPair)是找到哪两个向量(再次,每个被定义为具有10个值的数组)距离最近。当我在“main.cpp”文件中调用此函数时,我得到一个“没有匹配的函数,名为nearestPair”错误。

我很确定错误是在我传入函数的值中,或者是我尝试返回它的方式(通过将值打印到控制台)。

免责声明 - 如果需要转让:),那么对解决方案的暗示将非常受欢迎!

以下是我的文件:

main.cpp:

#include <iostream>
#include "double_arrays.h"

int main(int argc, const char * argv[])
{

//Define test values to test vectDistance
    double first[10] = {
        0.595500, 0.652927, 0.606763, 0.162761, 0.980752, 0.964772, 0.319322, 0.611325, 0.012422, 0.393489
    };
    double second[10] = {
        0.416132, 0.778858, 0.909609, 0.094812, 0.380586, 0.512309, 0.638184, 0.753504, 0.465674, 0.674607
    };

    //call vectDistance with test values, should equal 1.056238

    std::cout << "Euclidian distance is " << vectDistance(first, second) << std::endl;
    std::cout << "Should equal ~1.056238" << std::endl;

//Define test values for closestPair

    double a[10] = {
        0.183963, 0.933146, 0.476773, 0.086125, 0.566566, 0.728107, 0.837345, 0.885175, 0.600559, 0.142238
    };
    double b[10] = {
        0.086523, 0.025236, 0.252289, 0.089437, 0.382081, 0.420934, 0.038498, 0.626125, 0.468158, 0.247754
    };
    double c[10] = {
        0.969345, 0.127753, 0.736213, 0.264992, 0.518971, 0.216767, 0.390992, 0.242241, 0.516135, 0.990155
    };

//create 2D array to send to closestPair

    double** test = new double*[3];
    test[0] = a;
    test[1] = b;
    test[2] = c;

//output the values of the two vectors which are closest, in Euclidian distance
        std::cout << closestPair(test) << std::endl;

    return 0;
}

double_arrays.cpp:

#include "double_arrays.h"
#include <iostream>
#include <vector>
#include <math.h>

double vectDistance( double first[], double second[]) {
    int i = 0;
    double distance = 0.0;
    for (int j = 0; j < 10; ++j) {
        distance += pow((first[j] - second[i]), 2);
        ++i;
    }
    return distance = pow(distance, 0.5);
}

double** closestPair(double arrays[][10]) {
    double** closest = new double*[2];
    closest[0] = new double[10];
    closest[0] = arrays[0];
    closest[1] = new double[10];
    closest[1] = arrays[1];


    double minDistance = vectDistance(arrays[0], arrays[1]);

    for (int i = 0; i < 9; ++i){
        for (int j = i + 1; j < 10; ++j) {
            if (vectDistance(arrays[i], arrays[j]) < minDistance) {
                minDistance = vectDistance(arrays[i], arrays[j]);
                closest[0] = arrays[i];
                closest[1] = arrays[j];
            }
        }
    }

    return closest;
}

最后,头文件header.h:

#ifndef double_arrays_double_arrays_h
#define double_arrays_double_arrays_h

double vectDistance( double first[], double second[]);
double** closestPair(double arrays[][10]);

#endif

2 个答案:

答案 0 :(得分:1)

类型double**closestPair的参数类型不兼容。

closestPair的有效参数:

const int N = 20;
double arrays[N][10];  // Can use array to call closestPair

const int N = 20;
double (*arrays)[10] = new double[N][10];  // Can use array to call closestPair

答案 1 :(得分:1)

你的头文件不匹配,应该是:

#ifndef _DOUBLE_ARRAYS_H_
#define _DOUBLE_ARRAYS_H_

然后在你的double_arrays.cpp文件中调用:

#include "double_arrays.h"

要从.cpp文件的头文件中调用声明的函数,请尝试:

double ** Double_Arrays::closestPair(double arrays[][10]) {
/... your code.../
return closest;
}