1

助推器版本:1.45

编译器:Visual Studio 2008

平台:Windows 7

错误:编译以下代码时出现错误 C2440 "initializing" : cannot convert from 'boost::units::quantity' to 'boost::units::quantity' with

[ 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”的构造函数被声明为“显式”

[ Unit=myproject::types::myproject_length ]

我不确定这里有什么问题。如果我从以下代码中删除“radii_t result =”并注释掉 IO 语句,我会得到与上面引用的相同的错误。下面是我使用的代码。感谢您在我学习 Boost Units 时的耐心等待。

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;
}
4

1 回答 1

0
  1. The core problem is that your compiler message is truncated. In gcc, it reads:

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

However, in general boost-units compiler errors are a little ... verbose. Normally, I don't try to understand them, but rather use boost.units to verify that my units are correct, and if they aren't, have a long hard look at the code to determine what's wrong. In your case, you could compare the aka types and see that the signs for the length base dimensions differ.

For Q2, boost::units::power_typeof_helper is your friend, as well as the other type helpers.

于 2011-12-02T21:53:21.983 回答