我想正确编译以下代码:
#include <boost/units/systems/si.hpp>
#include <boost/units/base_units/metric/hour.hpp>
int main(int argc, char **argv) {
using namespace boost::units;
quantity<si::time> t = 2.0 * metric::hour_base_unit::unit_type();
}
使用gcc,我收到以下错误:
error: conversion from ‘boost::units::multiply_typeof_helper<double, boost::units::unit<boost::units::list<boost::units::dim<boost::units::time_base_dimension, boost::units::static_rational<1l> >, boost::units::dimensionless_type>, boost::units::heterogeneous_system<boost::units::heterogeneous_system_impl<boost::units::list<boost::units::heterogeneous_system_dim<boost::units::scaled_base_unit<boost::units::si::second_base_unit, boost::units::scale<60l, boost::units::static_rational<2l> > >, boost::units::static_rational<1l> >, boost::units::dimensionless_type>, boost::units::list<boost::units::dim<boost::units::time_base_dimension, boost::units::static_rational<1l> >, boost::units::dimensionless_type>, boost::units::dimensionless_type> >, void> >::type {aka boost::units::quantity<boost::units::unit<boost::units::list<boost::units::dim<boost::units::time_base_dimension, boost::units::static_rational<1l> >, boost::units::dimensionless_type>, boost::units::heterogeneous_system<boost::units::heterogeneous_system_impl<boost::units::list<boost::units::heterogeneous_system_dim<boost::units::scaled_base_unit<boost::units::si::second_base_unit, boost::units::scale<60l, boost::units::static_rational<2l> > >, boost::units::static_rational<1l> >, boost::units::dimensionless_type>, boost::units::list<boost::units::dim<boost::units::time_base_dimension, boost::units::static_rational<1l> >, boost::units::dimensionless_type>, boost::units::dimensionless_type> >, void>, double>}’ to non-scalar type ‘boost::units::quantity<boost::units::unit<boost::units::list<boost::units::dim<boost::units::time_base_dimension, boost::units::static_rational<1l> >, 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<10l, boost::units::static_rational<3l> > >, 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> > > > > > > > > > > >’ requested
quantity<si::time> t = 2.0 * metric::hour_base_unit::unit_type();
^
我错过了什么?
答案 0 :(得分:3)
快速查看文档意味着unit
类之间的大多数(如果不是全部)转换都是explicit
(它是design decision)。因此,您可以使用direct-initiliazation而不是copy-initialization:
#include <boost/units/systems/si.hpp>
#include <boost/units/base_units/metric/hour.hpp>
int main(int argc, char **argv) {
using namespace boost::units;
quantity<si::time,double> xxxxxx (2.0 * metric::hour_base_unit::unit_type());
}
它有效。
this answer中解释了在您的尝试没有解释时这种情况有效的原因。
希望有所帮助。