gpt4 book ai didi

c++ - 如何将 boost::units::metric::hour_base_unit 转换为 boost::units::si::time?

转载 作者:搜寻专家 更新时间:2023-10-31 01:43:15 26 4
gpt4 key购买 nike

我想让下面的代码正确编译:

#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();
^

我错过了什么?

最佳答案

快速浏览文档意味着大多数(如果不是全部)unit 类之间的转换是显式(它是 design decision )。因此,您可以使用直接初始化而不是复制初始化:

#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 中解释了此方法有效而您的尝试无效的原因。 .

希望对您有所帮助。

关于c++ - 如何将 boost::units::metric::hour_base_unit 转换为 boost::units::si::time?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25512706/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com