我正在尝试将 XmlRpc::XmlRpcValue 转换为双精度值。隐式转换方法不起作用,所以我必须显式转换它。但是,我无法让 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”类型到“double”类型的无效静态转换</p>
重新解释 - 错误:从类型“const XmlRpc::XmlRpcValue”到类型“double”的无效转换</p>
动态 - 不能 dynamic_cast '(& dof.XmlRpc::XmlRpcValue::operator)->XmlRpc::XmlRpcValue::operator'(类型为 'const class XmlRpc::XmlRpcValue')类型为 'double'(目标不是指针或参考)
如果有人可以帮助我,我将不胜感激。
编辑:
我尝试将 static_cast 转换为 std::vector< std::vector >。它无法编译:
~/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