2014-09-18 120 views
0

我是新来的c + +,相当肯定错误是在我传递给函数的变量。基本上,我在double_arrays.cpp文件中定义了两个函数 - 首先确定两个向量之间的欧几里德距离(以10个值的数组形式传入,此函数效果很好)。另一个(nearestPair)是查找哪两个向量(同样,每个被定义为具有10个值的数组)是距离最近的。当我在我的“main.cpp”文件中调用这个函数时,我得到了一个“没有匹配函数叫做closestPair”的错误。C++“没有匹配的函数错误”返回二维数组

我相当肯定,错误是在我传递给函数的值中,或者以我试图返回它的方式(通过将值打印到控制台)。

免责声明 - 这如果作业:),所以对解决方案的暗示将超过欢迎!

这里是我的文件:

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 
+1

如果你要硬编码的大小,你应该使用'常量int'而不是有很多的如果它发生变化,10个可以修改。你也确定'new double * [3]'匹配'double arrays [] [10]'? – crashmstr 2014-09-18 15:57:52

+1

提示:[std :: array](http://en.cppreference.com/w/cpp/container/array) – melak47 2014-09-18 15:58:32

+1

使用向量向量? – taocp 2014-09-18 15:59:14

回答

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

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

#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; 
}