C++ 将XmlRpcValue显式转换为double

C++ 将XmlRpcValue显式转换为double,c++,casting,xml-rpc,explicit-conversion,C++,Casting,Xml Rpc,Explicit Conversion,我试图将XmlRpc::XmlRpcValue转换为双精度值。隐式强制转换方法不起作用,因此我必须显式强制转换它。但是,我无法使3种显式转换类型中的任何一种工作 XmlRpcValue是一个双精度向量的向量。在.yaml文件中,该字段如下所示: DOF: [[0, 3.5], [0, 3.5], [-3.14159, 3.14159]] 现在我想把每一个值读入一个double。我尝试了以下方法: for(unsigned int i=0;i<dof.size();i++) { d

我试图将XmlRpc::XmlRpcValue转换为双精度值。隐式强制转换方法不起作用,因此我必须显式强制转换它。但是,我无法使3种显式转换类型中的任何一种工作

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

DOF: [[0, 3.5], [0, 3.5], [-3.14159, 3.14159]]
现在我想把每一个值读入一个double。我尝试了以下方法:

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

for(unsigned int i=0;i我意识到这个线程已经有两年多的历史了,但当我遇到这个问题时,这是google上的第一个结果。虽然它没有显示在上面的代码部分,但dof变量可能被声明为const

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

这意味着,如果希望检索存储在constxmlrpcvalue中的double的值,您有三个选项

  • 重写代码以删除常量
  • 将常量XmlValue复制到临时非常量XmlValue,然后检索双精度
  • 抛弃常数

  • 从误差来看,dof似乎包含XmlRpcValues的向量。你确定向量包含双精度吗?没错,dof是XmlRpcValues的向量。所以dof[i]是XmlRpcValues的向量,dof[i][j]是单个XmlRpcValues。我要遍历每个向量(dof[i])然后将第一个和第二个元素转换为double。这没有意义。这两种类型完全不相关。嗯,我当时错了。dof只是一个XmlRpc::XmlRpcValue,其值设置为double向量的向量(该值在问题中显示)。我尝试将dof静态转换为std::vector,但仍然出现错误。我编辑了带有错误的问题。问题是,转换不是这样工作的。您不能简单地将某个内容转换为不相关的类型。您可能想阅读以下内容:谢谢!从XmlRpcValue中获取双精度值花费了我比应该花费的时间更长的时间。
    ~/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