c++ - 无法使用 Boost Units 确定编译器错误的解决方案

标签 c++ boost boost-units

boost 单位版本:1.45

编译器:Visual Studio 2008

平台:Windows 7

错误:编译以下代码时出现错误 C2440“正在初始化”:无法使用<从“boost::units::quantity”转换为“boost::units::quantity”/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”类的构造函数被声明为“显式”

[ Unit=myproject::types::myproject_length ]

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

Q1:出现这个错误的原因是什么?错误输出有什么帮助 你来解决这个问题。

Q2:即使上面的错误不存在你提到它会 量纲分析失败。我想我们都同意类型是 半径^-1。您如何声明要 boost 到正确幂的类型?

斯蒂芬

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

最佳答案

  1. 核心问题是您的编译器消息被截断了。在 gcc 中,它显示为:

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类型并查看长度基本尺寸的符号不同。

对于第二季度,boost::units::power_typeof_helper是你的 friend ,也是其他类型的 helper 。

关于c++ - 无法使用 Boost Units 确定编译器错误的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8359167/

相关文章:

c++ - std::map 没有复制值

c++ - Boost.MPI 给出架构 x86_64 的 undefined symbol

c++ - 使用 boost chrono 格式化时间

c++ - 添加分钟和秒。以小时显示

c++ - 尝试使用 cmake 编译代码时 undefined reference

c++ - 重写 String 类中的下标运算符

C++ 在类中创建线程给出 c2064

c++ - Boost.Units 是如何得出这种不精确的转换结果的?

c++ - 使用代理模型

c++ - Boost 仅序列化 std::wstring 的第一个字符