0
#include <stdlib.h>
....
double returnDistance(string coord1, string coord2, const vector<string> vec) {
    int arr1[11], arr2[11];
    istringstream iss1(coord1);
    int i = 0;
    while(iss1) {
        iss1 >> arr1[i];
        i++;
    }
    istringstream iss2(coord2);
    i = 0;
    while(iss2) {
            iss2 >> arr2[i];
            i++;
    }
    //error below when calling atof
    return calculateDistance(atof(arr1[6]), atof(arr2[6]),
                             atof(arr1[7]), atof(arr2[7]),
                             atof(arr1[8]), atof(arr2[8]))
}

arr1[] and arr2[] are both arrays of strings and calculateDistance calculates the 3-D distance given x, y, z coordinates, but for some reason I get the error that "No matching function for call to 'atof'". Help please!

PS: I get the following error when I try using .c_str(): "Member reference base type 'int' is not a structure or union"

soochism
  • 13
  • 1
  • 5
  • 1
    What is the declaration of `arr1` and `arr2`? – Phillip Kinkade Nov 10 '13 at 01:02
  • When you say "strings", do you mean `char*`, optionally with some `const` qualification, or `std::string`? If you mean the latter, you'll need to use `.c_str()` on the objects. – Dietmar Kühl Nov 10 '13 at 01:03
  • I get the following error when I try using .c_str(): "Member reference base type 'int' is not a structure or union" – soochism Nov 10 '13 at 01:08
  • Are you saying that on of your arrays is declared as an array of `int`? You can't pass `int`s as argument to `atof()`. The function `atof()` takes a `char const*` as argument. – Dietmar Kühl Nov 10 '13 at 01:12
  • Would you be able to post relevant omitted code as it could give us more insight as to what is wrong – Nathan Wride Nov 10 '13 at 01:13
  • Sorry about that, I posted the relevant code above. – soochism Nov 10 '13 at 01:16
  • So, what made you think you need `atof`? You have integers, and they will be converted to floats just by passing them to the function. – Retired Ninja Nov 10 '13 at 01:19
  • Oh geez I declared the arrays as int arrays... Sorry about that – soochism Nov 10 '13 at 01:20
  • Dietmar Kuhl has it right. double atof(const char *s); is the definition of atof(). you really shouldn't pass an integer to atof() as a const char *, that is what the compiler is telling you by "No matching function for call to 'atof'" – Mark Hendrickson Nov 10 '13 at 01:22

2 Answers2

2

If I understand the goal of your code, you should update it as follows:

double arr1[11], arr2[11];
...
return calculateDistance(arr1[6], arr2[6],
                         arr1[7], arr2[7],
                         arr1[8], arr2[8]);

Basically, each arr?[?] is already an float and the string->float conversion is done by the >> operator.

Rémi
  • 679
  • 3
  • 7
  • Yeah, I just declared the arrays incorrectly as int instead of string – soochism Nov 10 '13 at 01:21
  • if you want to actually read floats, I'll update my answer (but still, I don't think you have to declare your arrays as strings). – Rémi Nov 10 '13 at 01:23
  • `atof()` returns a `double`, despite its name; it dates from the era in C's history when all arguments and return values were `double`. – Jonathan Leffler Nov 10 '13 at 01:37
0

The function atof() is for converting ASCII (C style strings) to double. You are passing integers; there isn't going to be an overload for that.

You can simply use casts (static_cast<double>(arr1[0]), etc), though you might also be able to call the function with the integers. Or you could provide a trivial inline function to do the conversion:

inline double itof(int i) { return i; }

I just declared the arrays incorrectly as int instead of string.

Careless, but why not declare the arrays as double? And check for array boundaries, too:

#include <vector>
#include <strstream>
#include <string>
using namespace std;

extern double calculateDistance(double x1, double y1, double x2, double y2, double x3, double y3);
double returnDistance(string coord1, string coord2, const vector<string> vec);

double returnDistance(string coord1, string coord2, const vector<string>)
{
    vector<double> arr1, arr2;
    istringstream iss1(coord1);
    int i1 = 0;
    while (iss1 && i1 < 9)  // No need to read beyond 9th number
        iss1 >> arr1[i1++];

    istringstream iss2(coord2);
    int i2 = 0;
    while (iss2 && i2 < 9)  // No need to read beyond 9th number
        iss2 >> arr2[i2++];

    if (i1 != i2 || i1 < 9)
        return 0.0;

    return calculateDistance(arr1[6], arr2[6], arr1[7], arr2[7], arr1[8], arr2[8]);
}

This compiles cleanly. Note that the const vector<string> argument to the main function is unused and hence unnamed in the function definition.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Yeah, I just declared the arrays incorrectly as int instead of string. Thanks! – soochism Nov 10 '13 at 01:21
  • OK; we're not mind readers — we can't tell when you write `int` and you mean `std::string` or `char *` or whatever. Be careful with your questions; it is unkind to make us work on the wrong question/situation. – Jonathan Leffler Nov 10 '13 at 01:23