首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用python将指针输入发送到c++程序?

如何使用python将指针输入发送到c++程序?
EN

Stack Overflow用户
提问于 2015-12-31 03:38:16
回答 1查看 155关注 0票数 1

我在c++中有一个返回向量的函数。我使用BOOST_PYTHON_MODULE在python中调用它。我想发送一个指针作为c++函数的输入。我试图将指针作为字符串发送。我知道这是最糟糕的方法,但有些人正在使用它,它工作得很好。对于我的情况,它不起作用。我是c++的新手。

工作案例:

代码语言:javascript
复制
#include <iostream>
#include <string.h>
#include <sstream>
using namespace std;

int main(){
    string s1 = "0x7fff3e8aee1c";
    stringstream ss;
    ss << s1;
    cout << "ss : " << ss << endl;
    long long unsigned int i;
    ss >> hex >> i;
    cout << "i : " << i << endl;
    int *i_ptr=reinterpret_cast<int *>(i);
    cout << "pointer: " << i_ptr << endl;
    return 0;
}

我的案例:

代码语言:javascript
复制
#include "openrave-core.h"
#include "ros/ros.h"
#include "std_msgs/String.h"
#include "valid_grasp_generator/GraspSnapshot.h"
#include <iostream>
#include <vector>
#include <boost/python.hpp>
#include <sstream>
using namespace OpenRAVE;
using namespace std;

boost::python::list get_penetration_depth(string env)
{
    vector<double> vec(9,0);
    stringstream ss(env);
    long long unsigned int i;
    ss >> hex >> i;
    EnvironmentBasePtr penv = reinterpret_cast<int *>(i);


    //code for getting penetration value from the openrave

    typename std::vector<double>::iterator iter;
    boost::python::list list;
    for (iter = vec.begin(); iter != vec.end(); ++iter) {
        list.append(*iter);
    }
    return list;
}

BOOST_PYTHON_MODULE(libdepth_penetration){
    using namespace boost::python;
    def("get_penetration", get_penetration_depth);
}

Catkin_make过程中出错:

代码语言:javascript
复制
/home/saurabh/catkin_ws/src/valid_grasp_generator/src/depth_penetration.cpp: In function ‘boost::python::list get_penetration_depth(std::string)’:
/home/saurabh/catkin_ws/src/valid_grasp_generator/src/depth_penetration.cpp:37:56: error: conversion from ‘int*’ to non-scalar type ‘OpenRAVE::EnvironmentBasePtr {aka boost::shared_ptr<OpenRAVE::EnvironmentBase>}’ requested
     EnvironmentBasePtr penv = reinterpret_cast<int *>(i);
                                                        ^
make[2]: *** [valid_grasp_generator/CMakeFiles/depth_penetration.dir/src/depth_penetration.cpp.o] Error 1
make[1]: *** [valid_grasp_generator/CMakeFiles/depth_penetration.dir/all] Error 2
make: *** [all] Error 2
Invoking "make -j8 -l8" failed
EN

回答 1

Stack Overflow用户

发布于 2016-01-31 19:25:49

在本例中,您尝试了从intshared_ptr的错误转换

代码语言:javascript
复制
EnvironmentBasePtr penv = reinterpret_cast<int *>(i); // wrong conversion
// where EnvironmentBasePtr is boost::shared_ptr<EnvironmentBase>

对于这个转换,我建议使用shared_ptra null deleter的构造函数。我看到了3种可能的解决方案。

您可以定义自己的 null deleter void deleter(void* ptr) {},然后:

代码语言:javascript
复制
boost::shared_ptr<EnvironmentBase> penv(reinterpret_cast<EnvironmentBase *>(i),&deleter);

如果您的boost是1.55或更低的,则可以包括<boost/serialization/shared_ptr.hpp>,然后:

代码语言:javascript
复制
boost::shared_ptr<EnvironmentBase> penv(reinterpret_cast<EnvironmentBase *>(i),boost::serialization::null_deleter());

如果您的boost为1.56或更高版本的,则可以包括<boost/core/null_deleter.hpp>,然后:

代码语言:javascript
复制
boost::shared_ptr<EnvironmentBase> penv(reinterpret_cast<EnvironmentBase *>(i),boost:null_deleter());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34536076

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档