luoshen_li 2016-01-10 03:11 采纳率: 0%
浏览 2276
已采纳

c++调用python函数报错

目标:用C++程序调用python模块里面的函数
python 模块: testm.py
函数:testm
具体实现如下:
python 模块代码
def testm(a,b=2):
c=a+b
return c

c++部分代码
#include"Python.h"
#include
#include
using namespace std;
void main()
{
bool c;
int a = 0;
Py_Initialize();
PyRun_SimpleString("print('hellow')");
PyObject*modle = NULL;
PyObject*func = NULL;
PyObject*parm = NULL;
PyObject*result=NULL;
modle = PyImport_ImportModule("testm");
if (!modle)
{
cout << "wrong pyfile" << endl;
system("pause");
}
func = PyObject_GetAttrString(modle, "testm");
if (!func)
{
cout << "wrong func" << endl;
system("pause");
}
parm = Py_BuildValue("i", 10);
result = PyEval_CallObject(func, parm);
PyArg_Parse(result, "i", &a);
cout << a << endl;
Py_DECREF(parm);
Py_DECREF(func);
Py_Finalize();
system("pause");
}

报错内容,a的值还是0,其余部分正常。
问题如下:
第一个,a为什么不是102;
第二个,能不能检验传递参数是否传递成功
第三个,如何传递列表与获得返回列表

  • 写回答

2条回答 默认 最新

  • xiajindong5 2016-01-10 04:03
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致