1

我使用这种方法来定义一个新的派生维度质量流量,它应该有单位kg / s

包括

#include <boost/units/io.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/systems/si/io.hpp>
#include <boost/units/systems/si/prefixes.hpp>    

namespace Units
{
  using namespace boost::units;

  namespace SI
  {
    using namespace boost::units::si;
  }

  template <class U> using Quantity = quantity<U>;

  typedef Quantity<SI::time> Time;
  typedef Quantity<SI::mass> Mass;

  typedef Units::derived_dimension<Units::mass_base_dimension, 1,
                                   Units::time_base_dimension, -1>::type MassFlowDimension;

  typedef Quantity<MassFlowDimension> MassFlowRate;

  BOOST_UNITS_STATIC_CONSTANT(kilogram_per_seconds, MassFlowRate);
}

我原以为从/到现有基本单位的转换可以开箱即用,但显然情况并非如此:

int main()
{
  // does work
  Units::MassFlowRate mass_flow = 1.0*(Units::kilogram_per_seconds);

  // does not work
  Units::MassFlowRate other_mass_flow = 1.0*(Units::SI::kilogram / Units::SI::second);

  // does not work either
  Units::Mass total_flow = mass_flow * (1.0*Units::SI::second);
}

我想使用派生维度来定义集成到现有框架中的数量。我是否必须调用一些宏才能转换为现有单位/从现有单位转换?

编辑:错误消息如下:

main.cc:39:44: error: conversion from ‘boost::units::multiply_typeof_helper<double, boost::units::unit<boost::units::list<boost::units::dim<boost::units::mass_base_dimension, boost::units::static_rational<1> >, boost::units::list<boost::units::dim<boost::units::time_base_dimension, boost::units::static_rational<-1> >, boost::units::dimensionless_type> >, boost::units::homogeneous_system<boost::units::list<boost::units::si::meter_base_unit, boost::units::list<boost::units::scaled_base_unit<boost::units::cgs::gram_base_unit, boost::units::scale<10, boost::units::static_rational<3> > >, boost::units::list<boost::units::si::second_base_unit, boost::units::list<boost::units::si::ampere_base_unit, boost::units::list<boost::units::si::kelvin_base_unit, boost::units::list<boost::units::si::mole_base_unit, boost::units::list<boost::units::si::candela_base_unit, boost::units::list<boost::units::angle::radian_base_unit, boost::units::list<boost::units::angle::steradian_base_unit, boost::units::dimensionless_type> > > > > > > > > >, void> >::type {aka boost::units::quantity<boost::units::unit<boost::units::list<boost::units::dim<boost::units::mass_base_dimension, boost::units::static_rational<1> >, boost::units::list<boost::units::dim<boost::units::time_base_dimension, boost::units::static_rational<-1> >, boost::units::dimensionless_type> >, boost::units::homogeneous_system<boost::units::list<boost::units::si::meter_base_unit, boost::units::list<boost::units::scaled_base_unit<boost::units::cgs::gram_base_unit, boost::units::scale<10, boost::units::static_rational<3> > >, boost::units::list<boost::units::si::second_base_unit, boost::units::list<boost::units::si::ampere_base_unit, boost::units::list<boost::units::si::kelvin_base_unit, boost::units::list<boost::units::si::mole_base_unit, boost::units::list<boost::units::si::candela_base_unit, boost::units::list<boost::units::angle::radian_base_unit, boost::units::list<boost::units::angle::steradian_base_unit, boost::units::dimensionless_type> > > > > > > > > >, void>, double>}’ to non-scalar type ‘Units::MassFlowRate {aka boost::units::quantity<boost::units::list<boost::units::dim<boost::units::mass_base_dimension, boost::units::static_rational<1> >, boost::units::list<boost::units::dim<boost::units::time_base_dimension, boost::units::static_rational<-1> >, boost::units::dimensionless_type> >, double>}’ requested
   Units::MassFlowRate other_mass_flow = 1.0*(Units::SI::kilogram / Units::SI::second);

main.cc:39:38: error: conversion from ‘boost::units::multiply_typeof_helper<boost::units::quantity<boost::units::list<boost::units::dim<boost::units::mass_base_dimension, boost::units::static_rational<1> >, boost::units::list<boost::units::dim<boost::units::time_base_dimension, boost::units::static_rational<-1> >, boost::units::dimensionless_type> >, double>, boost::units::quantity<boost::units::unit<boost::units::list<boost::units::dim<boost::units::time_base_dimension, boost::units::static_rational<1> >, boost::units::dimensionless_type>, boost::units::homogeneous_system<boost::units::list<boost::units::si::meter_base_unit, boost::units::list<boost::units::scaled_base_unit<boost::units::cgs::gram_base_unit, boost::units::scale<10, boost::units::static_rational<3> > >, boost::units::list<boost::units::si::second_base_unit, boost::units::list<boost::units::si::ampere_base_unit, boost::units::list<boost::units::si::kelvin_base_unit, boost::units::list<boost::units::si::mole_base_unit, boost::units::list<boost::units::si::candela_base_unit, boost::units::list<boost::units::angle::radian_base_unit, boost::units::list<boost::units::angle::steradian_base_unit, boost::units::dimensionless_type> > > > > > > > > > >, double> >::type {aka boost::units::quantity<boost::units::quantity<boost::units::unit<boost::units::list<boost::units::dim<boost::units::time_base_dimension, boost::units::static_rational<1> >, boost::units::dimensionless_type>, boost::units::homogeneous_system<boost::units::list<boost::units::si::meter_base_unit, boost::units::list<boost::units::scaled_base_unit<boost::units::cgs::gram_base_unit, boost::units::scale<10, boost::units::static_rational<3> > >, boost::units::list<boost::units::si::second_base_unit, boost::units::list<boost::units::si::ampere_base_unit, boost::units::list<boost::units::si::kelvin_base_unit, boost::units::list<boost::units::si::mole_base_unit, boost::units::list<boost::units::si::candela_base_unit, boost::units::list<boost::units::angle::radian_base_unit, boost::units::list<boost::units::angle::steradian_base_unit, boost::units::dimensionless_type> > > > > > > > > > >, boost::units::list<boost::units::dim<boost::units::mass_base_dimension, boost::units::static_rational<1> >, boost::units::list<boost::units::dim<boost::units::time_base_dimension, boost::units::static_rational<-1> >, boost::units::dimensionless_type> > >, double>}’ to non-scalar type ‘Units::Mass {aka boost::units::quantity<boost::units::unit<boost::units::list<boost::units::dim<boost::units::mass_base_dimension, boost::units::static_rational<1> >, boost::units::dimensionless_type>, boost::units::homogeneous_system<boost::units::list<boost::units::si::meter_base_unit, boost::units::list<boost::units::scaled_base_unit<boost::units::cgs::gram_base_unit, boost::units::scale<10, boost::units::static_rational<3> > >, boost::units::list<boost::units::si::second_base_unit, boost::units::list<boost::units::si::ampere_base_unit, boost::units::list<boost::units::si::kelvin_base_unit, boost::units::list<boost::units::si::mole_base_unit, boost::units::list<boost::units::si::candela_base_unit, boost::units::list<boost::units::angle::radian_base_unit, boost::units::list<boost::units::angle::steradian_base_unit, boost::units::dimensionless_type> > > > > > > > > > >, double>}’ requested
   Units::Mass total_flow = mass_flow * (1.0*Units::SI::second);
4

1 回答 1

1

你混淆了单位、尺寸和数量。

  • 这是你的维度,

    typedef Units::derived_dimension<Units::mass_base_dimension, 1,
                                     Units::time_base_dimension, -1>::type MassFlowDimension;
    

    您不能从维度创建数量:

    typedef Quantity<MassFlowDimension> MassFlowRate;
    

    而是先创建一个单元,然后从中创建一个维度:

  • 您的常数应该是 Unit 常数,而不是数量:

    typedef Quantity<MassFlowDimension> MassFlowRate;
    BOOST_UNITS_STATIC_CONSTANT(kilogram_per_seconds, MassFlowRate);
    

固定的

Live On Coliru

#include <boost/units/io.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/systems/si/io.hpp>

namespace Units {
    using namespace boost::units;
    namespace SI = boost::units::si;

    template <class U> using Quantity = quantity<U>;

    typedef quantity<si::mass> Mass;
    typedef quantity<si::time> Time;

    typedef derived_dimension<mass_base_dimension, 1, time_base_dimension, -1>::type MassFlowDimension;
    typedef unit<MassFlowDimension, si::system> MassFlow;
    typedef Quantity<MassFlow> MassFlowRate;
    BOOST_UNITS_STATIC_CONSTANT(kilogram_per_seconds, MassFlow);
}

#include <iostream>
int main() {
    Units::MassFlowRate const mass_flow       = 7.0*Units::kilogram_per_seconds;
    Units::MassFlowRate const other_mass_flow = 1.0*(Units::SI::kilogram / Units::SI::second);
    Units::Mass         const total_flow      = mass_flow * (1.0*Units::SI::second);

    std::cout << "mass_flow:       " << mass_flow       << "\n";
    std::cout << "other_mass_flow: " << other_mass_flow << "\n";
    std::cout << "total_flow:      " << total_flow      << "\n";
}

印刷

mass_flow:       7 kg s^-1
other_mass_flow: 1 kg s^-1
total_flow:      7 kg
于 2018-03-08T03:05:05.933 回答