显式转换XmlRpcValue为double

时间:2014-05-12 19:00:13

标签: c++ casting xml-rpc explicit-conversion

我正在尝试将XmlRpc :: XmlRpcValue转换为double值。隐式转换方法不起作用,所以我必须明确地转换它。但是,我无法使用任何3种显式投射类型。

XmlRpcValue是双精度矢量的矢量。在.yaml文件中,该字段如下所示:

DOF: [[0, 3.5], [0, 3.5], [-3.14159, 3.14159]]

现在我想将每个值读成双精度型。我尝试过以下方法:

for(unsigned int i=0;i<dof.size();i++) {
    double min = static_cast<double>(dof[i][0]);
    double max = static_cast<double>(dof[i][1]);
    //Do stuff

使用static_cast,reinterpret_cast和dynamic_cast。我不觉得我完全理解一切明显的种姓,但我认为static_cast就是我所需要的。我尝试了动态并重新解释只是为了看看会发生什么。他们都失败了以下消息:

  

static - 从'const XmlRpc :: XmlRpcValue'类型到static的static_cast无效   输入'double'

     

重新解释 - 错误:来自类型'const的无效转换   XmlRpc :: XmlRpcValue'键入'double'

     

动态 - 不能dynamic_cast'(&amp;   dof.XmlRpc :: XmlRpcValue ::运算) - &GT; XMLRPC :: XmlRpcValue ::运营商   ('const class XmlRpc :: XmlRpcValue'类型)输入'double'(目标   不是指针或参考)

如果有人可以帮助我,我会非常感激。

编辑:

我尝试将static_cast转换为std :: vector&lt; std :: vector&gt;。它无法编译:

~/my_path/main.cpp: In function ‘void setDOF(XmlRpc::XmlRpcValue)’:
~/my_path/main.cpp:64:97: error: conversion from ‘const XmlRpc::XmlRpcValue’ to ‘std::vector<std::vector<double> >::size_type {aka long unsigned int}’ is ambiguous
~/my_path/main.cpp:58:6: note: candidates are:
In file included from /opt/ros/hydro/include/ros/node_handle.h:51:0,
                 from /opt/ros/hydro/include/ros/ros.h:45,
                 from /home/sterlingm/ros_workspace/src/ramp/ramp_planner/src/main.cpp:1:
/opt/ros/hydro/include/XmlRpcValue.h:92:5: note: XmlRpc::XmlRpcValue::operator double&() <near match>
/opt/ros/hydro/include/XmlRpcValue.h:92:5: note:   no known conversion for implicit ‘this’ parameter from ‘const XmlRpc::XmlRpcValue*’ to ‘XmlRpc::XmlRpcValue*’
/opt/ros/hydro/include/XmlRpcValue.h:91:5: note: XmlRpc::XmlRpcValue::operator int&() <near match>
/opt/ros/hydro/include/XmlRpcValue.h:91:5: note:   no known conversion for implicit ‘this’ parameter from ‘const XmlRpc::XmlRpcValue*’ to ‘XmlRpc::XmlRpcValue*’
/opt/ros/hydro/include/XmlRpcValue.h:90:5: note: XmlRpc::XmlRpcValue::operator bool&() <near match>
/opt/ros/hydro/include/XmlRpcValue.h:90:5: note:   no known conversion for implicit ‘this’ parameter from ‘const XmlRpc::XmlRpcValue*’ to ‘XmlRpc::XmlRpcValue*’
In file included from /usr/include/c++/4.7/vector:65:0,
                 from /usr/include/boost/format.hpp:17,
                 from /usr/include/boost/math/policies/error_handling.hpp:30,
                 from /usr/include/boost/math/special_functions/round.hpp:14,
                 from /opt/ros/hydro/include/ros/time.h:58,
                 from /opt/ros/hydro/include/ros/ros.h:38,
                 from /home/sterlingm/ros_workspace/src/ramp/ramp_planner/src/main.cpp:1:
/usr/include/c++/4.7/bits/stl_vector.h:292:7: error:   initializing argument 1 of ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = std::vector<double>; _Alloc = std::allocator<std::vector<double> >; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = std::vector<double>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::vector<double> >]’
make[2]: *** [ramp/ramp_planner/CMakeFiles/ramp_planner.dir/src/main.cpp.o] Error 1

1 个答案:

答案 0 :(得分:0)

我意识到这个帖子已经有两年多了,但是当我遇到这个问题时,这是谷歌的第一个结果。虽然上面的代码部分没有显示,但dof变量可能被声明为const。

问题是允许您将XmlRpcValue视为double的方法不是const方法,因此不能与const XmlRpcValue一起使用。

这意味着如果你想检索存储在const XmlRpcValue中的double的值,你有三个选项。

  1. 重写代码以删除const
  2. 将const XmlValue复制到临时非const XmlValue然后检索double
  3. 抛弃const