main.cc
#include <iostream>
#include <cstdlib>
#include <string>
#include "gps.h"
using namespace std;
int main(int argc, char *argv[])
{
GPScoord gps1(12.0,13.0);
GPScoord gps2(12.0,13.0);
cout<<gps1.distance_to(gps2)<<endl;
return 0;
}
gps.h
#ifndef GPS_H
#define GPS_H
class GPScoord {
public:
GPScoord(double lat, double longt);
double lattitude1();
double longtitude1();
double rad_lat();
double rad_long();
double distance_to(const GPScoord& gps );
private:
double lattitude;
double longtitude;
};
#endif
gps.cc
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <math.h>
#include "gps.h"
using namespace std;
class GPScoord
{
private:
double lattitude;
double longtitude;
public:
GPScoord(double lat,double longt) {
lattitude=lat;
longtitude=longt;
}
public:
double lattitude1(){ return lattitude; }
double longtitude1(){ return longtitude; }
double rad_lat(){ return lattitude*M_PI/180; }
double rad_long(){ return longtitude*M_PI/180; }
double distance_to(const GPScoord& gps ){
double R = 6371e3;
double radlat = rad_lat();
double ralong = rad_long();
double radlat1 = gps.rad_lat();
double radlong1 = gps.rad_long();
double radlat2 = radlat-radlat1;
double radlong2 = ralong-radlong1;
double a = pow((sin(radlat2/2)),2)+cos(radlat)*
cos(radlat1)*pow((sin(radlong2)),2);
double c=2*atan2(sqrt(a),sqrt(1-a));
return R*c;
}
};
我收到以下错误:
/tmp/cc13HrVO.o: In function `main':
/home/mustafa/Desktop/main.cc:14: undefined reference to `GPScoord::GPScoord(double, double)'
/home/mustafa/Desktop/main.cc:15: undefined reference to `GPScoord::GPScoord(double, double)'
/home/mustafa/Desktop/main.cc:17: undefined reference to `GPScoord::distance_to(GPScoord const&)'
collect2: error: ld returned 1 exit status
我不明白这个错误,12.0不是双倍还是gps2不是类? 我认为这个问题出在gps.h中,但我没有发现这个错误。
答案 0 :(得分:1)
首先,您需要编译两个源文件,如下所示:
g ++ -Wall main.cc gps.cc
然后您将看到您收到重新定义类的错误,如解释here所述,可以通过在头文件中定义类然后将类的方法实现到源文件来修复,而无需不得不重新定义课程。
之后你会得到这样的错误:
gps.cc:21:19: error: member function 'rad_lat' not viable: 'this' argument has
type 'const GPScoord', but function is not marked const
double radlat = rad_lat();
^~~~~~~
正如编译器所说的那样发生,因为某些函数需要标记为const。将它们放在一起你会得到类似下面的代码:
gps.h:
#ifndef GPS_H
#define GPS_H
class GPScoord {
public:
GPScoord(double lat, double longt);
double lattitude1();
double longtitude1();
double rad_lat() const;
double rad_long() const;
double distance_to(const GPScoord& gps ) const;
private:
double lattitude;
double longtitude;
};
#endif
gps.cc:
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <math.h>
#include "gps.h"
using namespace std;
GPScoord::GPScoord(double lat,double longt) {
lattitude=lat;
longtitude=longt;
}
double GPScoord::lattitude1(){ return lattitude; }
double GPScoord::longtitude1(){ return longtitude; }
double GPScoord::rad_lat() const { return lattitude*M_PI/180; }
double GPScoord::rad_long() const { return longtitude*M_PI/180; }
double GPScoord::distance_to(const GPScoord& gps ) const {
double R = 6371e3;
double radlat = rad_lat();
double ralong = rad_long();
double radlat1 = gps.rad_lat();
double radlong1 = gps.rad_long();
double radlat2 = radlat-radlat1;
double radlong2 = ralong-radlong1;
double a = pow((sin(radlat2/2)),2)+cos(radlat)*
cos(radlat1)*pow((sin(radlong2)),2);
double c=2*atan2(sqrt(a),sqrt(1-a));
return R*c;
}