Classes in gauges [Solved]

DragonflightDesign

Resource contributor
#1
You might not believe this, but I've never really strayed from the path of C over the years. Now I find I'm having to move into C++ and it's becoming a mild nightmare. Google is just not helping with what I know is a relatively simple question (it's probably one of those 'but you should already know it' things). I can't figure out how to get into 'calc_distance' and return the calculated distance to the outside world. The variable 'distance' itself is being recognised as the intellisense will give me 'distance.distance' once the class has been instantiated, but it won't show 'distance.calc_distance'. What did I miss (or misunderstand :))?

Code:
// Calling code
getLatLonDist distance(0.592539, 0.709186, 2.066470, 1.287762);

// Class
class getLatLonDist
{
    double lat1, lat2, lon1, lon2;

public:
    getLatLonDist(double, double, double, double);

    double distance = 0;

    double calc_distance(double latA, double latB, double lonA, double lonB)
    {
        double dlon, dlat;

        lat1 = latA;
        lat2 = latB;
        lon1 = lonA;
        lon2 = lonB;

        dlat = lat2 - lat1;
        dlon = lon2 - lon1;
        distance = acos(sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos(lon1 - lon2));

        return distance;
    }
};

getLatLonDist::getLatLonDist(double latitudeA, double latitudeB, double longitudeA, double longitudeB)
{
    lat1 = latitudeA;
    lat2 = latitudeB;
    lon1 = longitudeA;
    lon2 = longitudeB;
}
 

ddawson

Resource contributor
#2
What happens if you ignore the petulance of Intellisense and just bash on regardless:
double ThisDistance = distance.calc_distance(W,X,Y,Z);
My experience has been that Intellisense isn't really. It is rather easily confused by changes to your code. I find myself manually deleting the huge database files that VS creates for each project and forcing them to be rebuilt.

Doug
 

DragonflightDesign

Resource contributor
#3
I'm at work now Doug, so I can't be accurate. I tried that:
Code:
getLatLonDist distance();
double newDistance= distance.calc_distance(0.592539, 0.709186, 2.066470, 1.287762);
and got an error along the lines of 'missing class identifier'. I'll check for the exact error message when I get home again.
 

ddawson

Resource contributor
#4
You've declared the constructor as taking 4 input arguments, so your declaration
getLatLonDist distance();
will fail.
This is what is getting you in trouble:
Code:
getLatLonDist::getLatLonDist(double latitudeA, double latitudeB, double longitudeA, double longitudeB)
{
    lat1 = latitudeA;
    lat2 = latitudeB;
    lon1 = longitudeA;
    lon2 = longitudeB;
}
You don't need to be passing values into the constructor method - the calc_distance method will presumably be called any number of times with different input values.
Passing values into a constructor method is usually useful when the information being provided is unknown at compile time, but which will not change over the life of the class instance.
I would delete it entirely, and go with a class definition that looks like this:
Code:
// Class
class getLatLonDist
{
    double lat1, lat2, lon1, lon2;

public:
    double distance = 0;

    double calc_distance(double latA, double latB, double lonA, double lonB)
    {
        double dlon, dlat;

        lat1 = latA;
        lat2 = latB;
        lon1 = lonA;
        lon2 = lonB;

        dlat = lat2 - lat1;
        dlon = lon2 - lon1;
        distance = acos(sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos(lon1 - lon2));

        return distance;
    }
};
Then you would declare you class instance without any arguments:
getLatLonDist distance;

Doug
 
#5
maybe this will help

Code:
#include <math.h>



// Class
class LatLonDist
{
private:
    double lat1, lat2, lon1, lon2;

public:
    double distance = 0;

    //LatLonDist();
    LatLonDist::LatLonDist(double alat1, double alat2, double alon1, double alon2)
    {
        lat1 = alat1;
        lat2 = alat2;
        lon1 = alon1;
        lon2 = alon2;
    }


    double LatLonDist::calc_distance()
    {
        double dlon, dlat;
        dlat = lat2 - lat1;
        dlon = lon2 - lon1;
        distance = acos(sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos(lon1 - lon2));

        return distance;
    }
};

int main()
{
    LatLonDist mylatlon(0.592539, 0.709186, 2.066470, 1.287762);

    double res = mylatlon.calc_distance();

};
Edit: I see Doug beat me to it.
 

DragonflightDesign

Resource contributor
#6
Thank you gentlemen. I shall give one (or both) a try when I get home.

Doug: This:-

"Passing values into a constructor method is usually useful when the information being provided is unknown at compile time"

is freely banded about in all the constructor examples I found, but not one of them had the sting in the tail:

"but which will not change over the life of the class instance."

Thank you.
 

DragonflightDesign

Resource contributor
#8
I shall endeavour to de-confuzzle you Ed. They's just plug-in numbers with a known return for testing. I stole them from Ed Williams' Aviation Formulary - first one is LAX, second one is JFK, in radians.
 
Last edited:
Top