gpt4 book ai didi

c++ - 显式转换 XmlRpcValue 以加倍

转载 作者:行者123 更新时间:2023-11-28 06:52:42 26 4
gpt4 key购买 nike

我正在尝试将 XmlRpc::XmlRpcValue 转换为 double 值。隐式转换方法不起作用,所以我将不得不显式转换它。但是,我无法使 3 种显式转换类型中的任何一种起作用。

XmlRpcValue 是 double vector 的 vector 。在 .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

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

static - invalid static_cast from type ‘const XmlRpc::XmlRpcValue’ to type ‘double’

reinterpret - error: invalid cast from type ‘const XmlRpc::XmlRpcValue’ to type ‘double’

dynamic - cannot dynamic_cast ‘(& dof.XmlRpc::XmlRpcValue::operator)->XmlRpc::XmlRpcValue::operator’ (of type ‘const class XmlRpc::XmlRpcValue’) to type ‘double’ (target is not pointer or reference)

如果有人能帮我解决这个问题,我将不胜感激。

编辑:

我尝试将 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

最佳答案

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

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

这意味着如果您希望检索存储在 const XmlRpcValue 中的 double 值,您有三个选择。

  1. 重写您的代码以删除常量
  2. 将 const XmlValue 复制到一个临时的非 const XmlValue,然后取回 double
  3. 抛弃const

关于c++ - 显式转换 XmlRpcValue 以加倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23616789/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com