我正在尝试从[计算纬度/经度点之间的距离、方位等][1] 中对公式进行类型安全的 C++ 实现。不幸的是,我遇到了 C++ 类型错误,这些错误将实际问题隐藏在无休止的模板扩展之下。我想我真的很接近:
#include <iostream>
#include <boost/units/systems/angle/degrees.hpp>
#include <boost/units/systems/si/length.hpp>
#include <boost/units/systems/si/plane_angle.hpp>
#include <boost/units/systems/si/prefixes.hpp>
#include <boost/units/cmath.hpp>
#include <boost/units/io.hpp>
using namespace boost::units;
using namespace boost::units::si;
struct Position
{
public:
using angle_type = quantity<degree::plane_angle>;
Position(const angle_type &latitude, const angle_type &longitude) : m_latitude{latitude}, m_longitude{longitude} {}
angle_type m_latitude;
angle_type m_longitude;
};
const quantity<si::length> earth_radius{6371. * kilo * meters};
Position move(const Position &start, quantity<degree::plane_angle> bearing, quantity<si::length> distance)
{
const auto angular_distance = distance / earth_radius;
auto latitude2 = asin(sin(start.m_latitude) * cos(angular_distance) +
cos(start.m_latitude) * sin(angular_distance) * cos(bearing));
auto longitude2 = start.m_longitude + atan2(sin(bearing) * sin(angular_distance) * cos(start.m_latitude),
cos(angular_distance) - sin(start.m_latitude) * sin(latitude2));
return Position(latitude2, longitude2);
}
int main(int, char **)
{
return 0;
}
错误信息:
/home/jpo/src/units/main.cpp:30:41: error: no match for ‘operator+’
(operand types are ‘const angle_type’ and ‘double’)
30 | auto longitude2 = start.m_longitude + atan2(sin(bearing) * sin(angular_distance) * cos(start.m_latitude),
| ~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| | |
| | double
| const angle_type