Jep*_*sen 5 c++ boost boost-units
我正在尝试为米和公里创建单位.我想要总结并相应地转换它们.我知道boost :: units库已经有了SI系统,但我想从头开始创建,因为那时我需要为我的项目创建自己的系统(所以我这样做是为了学习).我的目的是声明一个可以使用单位修改的"长度"变量:例如我想写
Length xLength1 = 5350 * Meters + 2 Kilometers;
Run Code Online (Sandbox Code Playgroud)
为此,我创建了length.h文件,其中包含米和公里的定义,最后我声明了这两个单元之间的转换:
#ifndef LENGTH_H_
#define LENGTH_H_
#include <boost/units/base_dimension.hpp>
#include <boost/units/base_unit.hpp>
#include <boost/units/scaled_base_unit.hpp>
#include <boost/units/quantity.hpp>
#include <boost/units/conversion.hpp>
struct LengthBaseDimension : boost::units::base_dimension<LengthBaseDimension,1>{};
typedef LengthBaseDimension::dimension_type LengthDimension;
struct MeterBaseUnit : boost::units::base_unit<MeterBaseUnit, LengthDimension, 1>{};
template <> struct boost::units::base_unit_info<MeterBaseUnit>
{
static std::string name() { return "meter"; }
static std::string symbol() { return "m"; }
};
struct KilometerBaseUnit : boost::units::base_unit<KilometerBaseUnit, LengthDimension, 2>{};
template <> struct boost::units::base_unit_info<KilometerBaseUnit>
{
static std::string name() { return "kilometer"; }
static std::string symbol() { return "km"; }
};
// I don't want to use scaled_unit because I need this for my real purpose
BOOST_UNITS_DEFINE_CONVERSION_FACTOR(KilometerBaseUnit, MeterBaseUnit, double, 1000.0);
#endif
Run Code Online (Sandbox Code Playgroud)
然后我创建文件units.h,我在其中定义自己的单位系统
#ifndef LIB_UNITS_H_
#define LIB_UNITS_H_
#include "length.h"
#include <boost/units/unit.hpp>
#include <boost/units/static_constant.hpp>
#include <boost/units/make_system.hpp>
#include <boost/units/io.hpp>
typedef boost::units::make_system<MeterBaseUnit>::type UnitsSystem;
typedef boost::units::unit<boost::units::dimensionless_type, UnitsSystem> Dimensionless;
typedef boost::units::unit<LengthDimension , UnitsSystem> SystemLength;
BOOST_UNITS_STATIC_CONSTANT(Kilometer , SystemLength);
BOOST_UNITS_STATIC_CONSTANT(Kilometers , SystemLength);
BOOST_UNITS_STATIC_CONSTANT(Meter , SystemLength);
BOOST_UNITS_STATIC_CONSTANT(Meters , SystemLength);
// Typedefs of dimensions
typedef boost::units::quantity<SystemLength> Length;
#endif
Run Code Online (Sandbox Code Playgroud)
至少,我在我的main函数中使用了这个头文件
#include "units.h"
#include <iostream>
int main(void)
{
Length xLength1 ( 300.0 * Meters);
Length xLength2 (1500.0 * Kilometer );
Length xLength3;
std::cout << xLength2 << std::endl;
xLength3 = xLength1 + xLength2;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个项目编译,但它没有做我想要的.当我打印变量xLength2时,我得到1500米而不是1500公里或1500000米.总和也是错误的,因为我承认1800米.这就像我认为公里为米,转换不起作用.
我做错了什么?
除非您的项目是复制 Boost 库,否则这似乎是一种疯狂的单元系统编码方式。模板黑客很少是最好的选择。
相反,使用一个类!下面不是真正的代码:
class temperature{
double temp; //always in kelvin
temperature(double temperature, char unit){
if(unit=='k')
temp=temperature
if(unit=='c')
temp=temperature+273
}
};
Run Code Online (Sandbox Code Playgroud)
依此类推,实现转换回您想要的任何单位。基本上,您选择一个存储所有内容的基本单位,并与其进行转换。如果您使用 SI,可能是克、米、秒、开尔文等。