C++ C++;:如何在不获取SIGSEV错误的情况下访问结构
我正在编写一个小的实用程序类,它将纬度和经度转换为UTM本地系统。对于我正在使用的这个任务。我创建了一些C++ C++;:如何在不获取SIGSEV错误的情况下访问结构,c++,c++11,data-structures,struct,C++,C++11,Data Structures,Struct,我正在编写一个小的实用程序类,它将纬度和经度转换为UTM本地系统。对于我正在使用的这个任务。我创建了一些struct来帮助我管理大部分数据,但是如果我以特定的方式传递数据,就会出现问题。如果我清楚地重新声明这些值,它确实有效。请参见下面的示例: zoneconverter.h #ifndef ZONE_CONVERTER_H #define ZONE_CONVERTER_H #include <string> #include <math.h> #include <
struct
来帮助我管理大部分数据,但是如果我以特定的方式传递数据,就会出现问题。如果我清楚地重新声明这些值,它确实有效。请参见下面的示例:
zoneconverter.h
#ifndef ZONE_CONVERTER_H
#define ZONE_CONVERTER_H
#include <string>
#include <math.h>
#include <cmath>
#include <ctgmath>
#include <stdlib.h>
#include <stdio.h>
#include <stdexcept>
#define PI 3.14159265358979323846 /* pi */
#define SMaxA 6378137.0 /* semi major axis */
#define SMinA 6356752.314245 /* sdmi minor axis */
#define grid_size 100000.0 /* 100 km grid*/
struct Deg2Rad {
double D2R = PI/180.0;
};
struct Rad2Deg {
double R2D = PI*180.0;
};
// definition of the World Geodetic System 84
struct WGS84_DATA
{
double semi_major_axis_a = 6378137.0; // by definition
double semi_minor_axis_b = 6356752.314245; // by definition
const double flattening = (SMaxA-SMinA)/SMaxA; // by definition
const double first_eccentricity = 0.081891909; // by calculation
double second_eccentricity = 0.0820944377; // by calculation
double angular_velocity_earth = 72.92115e-6; // rad/s
double gravitational_constant = 3986004.418e8; // by definition
};
struct UTM_DATA
{
double point_scale_factor = 0.9996; // by convention
double equatorial_radius = 6378137.0; // meters also semi_major_axis_a
double inverse_flattening = 1/((SMaxA-SMinA)/SMaxA); // by convention
double northen_emisphere = 0.0; // meter
double southern_hemisphere = 10000000.0; // meter
double false_esting = 500000.0; // meter by convention
double first_eccentricity_power2 = 0.081891909*0.081891909;
double first_eccentricity_power4 = 0.081891909*0.081891909*0.081891909*0.081891909;
double first_eccentricity_power6 = 0.081891909*0.081891909*0.081891909*0.081891909*0.081891909*0.081891909;
};
enum UTMidentifierLeter {
X, W, V, U, T, S, R, Q, P, N,
M, L, K, J, H, G, F, E, D, C, Z
};
struct UTM_LETTER_ZONE { UTMidentifierLeter utmLetterZone; };
enum UTMIdentifierZone { NORWAY, SVALBARD };
struct UTM_ZONE { UTMIdentifierZone utmZone; };
class ZONE_converter
{
public:
ZONE_converter();
WGS84_DATA wgs84_data;
UTM_DATA utm_data;
Deg2Rad degreeToRad_reader;
Rad2Deg radToDeg_reader;
void UTM(double lat, double lon, double eastingUtmzone, double northingUtmzone);
char adjustForNorway(double lat);
char adjustForSvalbard(double lat, double lon);
char allOtherZones(double lat);
private:
UTM_LETTER_ZONE letter;
UTM_ZONE zone;
double latitude;
double longitude;
int current_zone;
};
#endif // ZONE_CONVERTER_H
有人能解释一下这个问题吗?一个问题是,您在这里使用的是未初始化的变量:
double eastingUtmzone; // uninitialized
double northingUtmzone; // uninitialized
convert.UTM(lat, lon, eastingUtmzone, northingUtmzone);
std::cout<< lat << lon<< northingUtmzone<< eastingUtmzone<< std::endl;
double eastingUtmzone;//未初始化
双北区;//未初始化
转换为UTM(纬度、经度、东印古特区、北印古特区);
std::cout(SMaxA SMinA)/SMaxA
——这些是变量还是常量?你也检查过你所有的值,以确保它们不是无效的double、nan等吗?@PaulMcKenzie,感谢你阅读这个问题,我在标题中定义了它们,请参阅更新的codeFYI——请注意,你发布的大部分内容可能是常量double
,而不仅仅是double
。类似于ZONE\u converter::UTM
的内容都是常量数据。SIGSEGV
表示内存错误,很可能是试图取消对空指针或无效指针的引用。您需要使用调试器来帮助您找到代码中崩溃发生的位置,并在该位置检查所有相关变量的值。@Emanuele您的Zone_converter
构造函数无法初始化您的成员变量。那是一面红旗。请发一封信。
#include <iostream>
#include "zone_converter.h"
using namespace std;
int main()
{
ZONE_converter convert;
double lat = 26.281742;
double lon = 92.142683;
double eastingUtmzone;
double northingUtmzone;
convert.UTM(lat, lon, eastingUtmzone, northingUtmzone);
std::cout<< lat << lon<< northingUtmzone<< eastingUtmzone<< std::endl;
return 0;
}
void ZONE_converter::UTM(double lat, double lon, double eastingUtmzone, double northingUtmzone)
{
double m0_a11 = (std::pow(wgs84_data.first_eccentricity, 2)/4);
double m0_a12 = (std::pow(wgs84_data.first_eccentricity, 4)/64);
double m0_a13 = (std::pow(wgs84_data.first_eccentricity, 6))/256;
double m0 = 1 - m0_a11 - 3*m0_a12 - 5*m0_a13;
// ... additional operation
}
double eastingUtmzone; // uninitialized
double northingUtmzone; // uninitialized
convert.UTM(lat, lon, eastingUtmzone, northingUtmzone);
std::cout<< lat << lon<< northingUtmzone<< eastingUtmzone<< std::endl;