提升单位版本:1.45
编译器:Visual Studio 2008
平台:Windows 7
错误:编译以下代码时,我收到错误C2440“初始化”:无法从'boost :: units :: quantity'转换为'boost :: units :: quantity'with < / p>
[
Unit=boost::units::unit>,boost::units::detail::static_power_impl<0>::apply>::type,boost::units::hogogeneous_system>>>>,
Y = double
]
和
[
Unit=myproject::types::myproject_length
]
类'boost :: units :: quantity'的构造函数用
声明为'explicit' [
Unit=myproject::types::myproject_length
]
我不确定这里有什么问题。如果我从以下代码中删除“radii_t result =”并注释掉IO语句,我会得到与上面引用的相同的错误。 以下是我使用的代码。当我学习Boost时,我感谢你的耐心 单元。
Q1:出现此错误的原因是什么?错误输出中有什么帮助 你要明白这一点。
Q2:即使上述错误不存在,你也提到了 没有进行尺寸分析。我想我们都同意这种类型 半径^ -1。如何声明要提升到正确功率的类型?
斯蒂芬
#include <boost/units/base_unit.hpp>
#include <boost/units/base_units/angle/radian.hpp>
#include <boost/units/io.hpp>
#include <boost/units/make_system.hpp>
#include <boost/units/physical_dimensions/length.hpp>
#include <boost/units/physical_dimensions/time.hpp>
#include <boost/units/static_constant.hpp>
#include <iostream>
namespace myproject {
namespace types {
//-------------------------------------
// Base dimensions
//-------------------------------------
struct length_base_dimension : public boost::units::base_dimension<length_base_dimension,1> {};
//-------------------------------------
// Dimensions
//-------------------------------------
struct radii_base_unit : public boost::units::base_unit<radii_base_unit, boost::units::length_dimension, 1>
{
static std::string name() { return("radii"); }
static std::string symbol() { return("r"); }
};
struct minute_base_unit : public boost::units::base_unit<minute_base_unit,boost::units::time_dimension,3>
{
static std::string name() { return ("minute"); }
static std::string symbol() { return ("min"); }
};
typedef boost::units::make_system<radii_base_unit,
minute_base_unit,
boost::units::angle::radian_base_unit >::type myproject_system_t;
typedef boost::units::unit<boost::units::length_dimension,myproject_system_t> myproject_length;
typedef boost::units::quantity<myproject_length, double> radii_t;
BOOST_UNITS_STATIC_CONSTANT(radii,myproject_length);
}
}
int main ( int, char** )
{
using namespace myproject::types;
radii_t val1 ( 5 * radii );
radii_t val2 ( 3 * radii );
radii_t result = 1.0 / ( val1 - val2 );
std::cout << result << std::endl;
return 0;
}
答案 0 :(得分:0)
54:40: error: conversion from ‘boost::units::divide_typeof_helper<double, boost::units::quantity<boost::units::unit<boost::units::list<boost::units::dim<boost::units::length_base_dimension, boost::units::static_rational<1l> >, boost::units::dimensionless_type>, boost::units::homogeneous_system<boost::units::list<boost::units::angle::radian_base_unit, boost::units::list<myproject::types::radii_base_unit, boost::units::list<myproject::types::minute_base_unit, boost::units::dimensionless_type> > > > >, double> >::type {aka boost::units::quantity<boost::units::unit<boost::units::list<boost::units::dim<boost::units::length_base_dimension, boost::units::static_rational<-0x00000000000000001l> >, boost::units::dimensionless_type>, boost::units::homogeneous_system<boost::units::list<boost::units::angle::radian_base_unit, boost::units::list<myproject::types::radii_base_unit, boost::units::list<myproject::types::minute_base_unit, boost::units::dimensionless_type> > > >, void>, double>}’ to non-scalar type ‘myproject::types::radii_t {aka boost::units::quantity<boost::units::unit<boost::units::list<boost::units::dim<boost::units::length_base_dimension, boost::units::static_rational<1l> >, boost::units::dimensionless_type>, boost::units::homogeneous_system<boost::units::list<boost::units::angle::radian_base_unit, boost::units::list<myproject::types::radii_base_unit, boost::units::list<myproject::types::minute_base_unit, boost::units::dimensionless_type> > > > >, double>}’ requested
但是,一般来说,boost-units编译器错误有点......冗长。通常情况下,我不会尝试理解它们,而是使用boost.units来验证我的单位是否正确,如果不正确,请仔细查看代码以确定错误。在您的情况下,您可以比较aka
类型,并查看长度基本尺寸的符号不同。
对于Q2,boost::units::power_typeof_helper
是您的朋友,以及其他类型的助手。