0

I'm new to c++, and fairly sure the error is in the variables I'm passing into the function. Essentially, I have two functions defined in a double_arrays.cpp file - first is to determine the Euclidian distance between two vectors (passed in as arrays with 10 values, this function works great). The other (closestPair) is to find which two vectors (again, each being defined as an array with 10 values) are closest in distance. When I am calling this function in my "main.cpp" file, I am getting a "no matching function called closestPair" error.

I am fairly sure that the error is either in the values I am passing into the function, or in the way I am trying to return it (by printing the values to the console).

DISCLAIMER - THIS IF FOR AN ASSIGNMENT :), so hints towards a solution will be more than welcome!

Here are my files:

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

And, finally, the header file, 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
4
  • 1
    If you are going to hard-code sizes, you should use a const int instead of having a lot of 10s to modify if it changes. Also you sure new double*[3] matches double arrays[][10]? Commented Sep 18, 2014 at 15:57
  • 1
    hint: std::array Commented Sep 18, 2014 at 15:58
  • 1
    use vector of vectors instead? Commented Sep 18, 2014 at 15:59
  • 1
    The other thing, what should std::cout do with a double**? (i.e. how should it print that?) Commented Sep 18, 2014 at 16:02

2 Answers 2

1

The type double** is not compatible with the argument type of closestPair.

Valid arguments to closestPair:

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

or

const int N = 20;
double (*arrays)[10] = new double[N][10];  // Can use array to call closestPair
Sign up to request clarification or add additional context in comments.

Comments

1

Your header files does not match, it should be:

#ifndef _DOUBLE_ARRAYS_H_
#define _DOUBLE_ARRAYS_H_

Then on your double_arrays.cpp file call:

#include "double_arrays.h"

To call your declared functions from header file on your .cpp file try:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.