简体   繁体   English

C++ 中的问题:错误:“。”之前的预期主表达式令牌

[英]Problem in C++: error: expected primary-expression before ‘.’ token

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". 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.此 function 计算两个位置从源到目的地的距离。

Then I get the error message and I have no idea to apply this function.然后我收到错误消息,我不知道要应用这个 function。

This is the "distance" function from class "GPS_DD":这是来自 class “GPS_DD”的“距离” function:

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?不管“GeoDataSource_distance”如何,为了在“getDistance”function中应用,应该执行什么代码?

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).您需要 GPS_DD class 的实例,或在 static 方法中转换 GPS_DD::distance (但您不能在 static 方法中使用它)。

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:您需要像这样实例化 GPS_DD class :

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

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 .计算tr_src_wheretr_dst_where之间的距离。

The right syntax to call a method on an object is pretty basic.在 object 上调用方法的正确语法非常基本。 Time to revise the fundamentals?是时候修改基本面了?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM