0

In this member function of class transcation, I use the "distance" function from another cpp program and that function is also a member of class "GPS_DD". My code is like below:

double

Transaction::getDistance()
`{ return GPS_DD.distance(tr_src_where, tr_dst_where);}                                                                                                        

This function calculates two locations' distance from source to destination.

Then I get the error message and I have no idea to apply this function.

This is the "distance" function from class "GPS_DD":

double
GPS_DD::distance(GPS_DD& another)  {return GeoDataSource_distance(this->latitude, this->longtitude, 
 another.latitude, another.longtitude, 'M');

}

Regardless of the "GeoDataSource_distance, what code should be implemented in order to apply in the "getDistance" function?

  • Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please [edit] your question to include a [mcve] of the failing code (with context), and with comments on the lines where you get the errors. Also included should be a full and complete copy-paste of the error output. – Some programmer dude Apr 20 '20 at 06:15
  • Your call doesn't even match the prototype. I suspect that you need `tr_src_where.distance(tr_dst_where)` and [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Apr 20 '20 at 06:17
  • Okay! I just realized that tr_src_where itself is the GPS_DD class, and the argument is used for destination. I run this code and it successes, thanks! – William Wang Apr 20 '20 at 06:33

2 Answers2

1

You need an instance of GPS_DD class, or transform GPS_DD::distance in a static method (but you cannot use this inside a static method).

for example: instead of:

Transaction::getDistance()
{return GPS_DD.distance(tr_src_where, tr_dst_where);}    

you need to instantiate the GPS_DD class like this:

Transaction::getDistance()
{ GPS_DD anInstanceOfGPS_DD;
  return anInstanceOfGPS_DD.distance(tr_src_where, tr_dst_where);} 
SeventhSon84
  • 364
  • 1
  • 4
0

It's pretty clear from the names involved that what you are looking for is

double Transaction::getDistance()
{
    return tr_src_where.distance(tr_dst_where);
}

to calculate the distance between tr_src_where and tr_dst_where.

The right syntax to call a method on an object is pretty basic. Time to revise the fundamentals?

john
  • 85,011
  • 4
  • 57
  • 81