Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PyBytes作为字符串意外输出 我试图用C++嵌入Python。下面是用C++编写的代码。代码应该返回“hello_world”作为输出。但PyBytes_AS_字符串不会返回“hello_world”。我正在使用Python3.5版本,并尝试在Visu_Python_C++ - Fatal编程技术网

PyBytes作为字符串意外输出 我试图用C++嵌入Python。下面是用C++编写的代码。代码应该返回“hello_world”作为输出。但PyBytes_AS_字符串不会返回“hello_world”。我正在使用Python3.5版本,并尝试在Visu

PyBytes作为字符串意外输出 我试图用C++嵌入Python。下面是用C++编写的代码。代码应该返回“hello_world”作为输出。但PyBytes_AS_字符串不会返回“hello_world”。我正在使用Python3.5版本,并尝试在Visu,python,c++,Python,C++,PyBytes作为字符串意外输出 我试图用C++嵌入Python。下面是用C++编写的代码。代码应该返回“hello_world”作为输出。但PyBytes_AS_字符串不会返回“hello_world”。我正在使用Python3.5版本,并尝试在VisualStudio中编译代码 string myfunc(char * inp) { Py_Initialize(); PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pVa

PyBytes作为字符串意外输出 我试图用C++嵌入Python。下面是用C++编写的代码。代码应该返回“hello_world”作为输出。但PyBytes_AS_字符串不会返回“hello_world”。我正在使用Python3.5版本,并尝试在VisualStudio中编译代码

string myfunc(char * inp)
{
    Py_Initialize();
    PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue1;
    pName = PyUnicode_FromString("mod1");
    pModule = PyImport_Import(pName);
    pDict = PyModule_GetDict(pModule);
    pFunc = PyDict_GetItemString(pDict, "func1");
    pArgs = PyTuple_New(1);
    pValue1 = PyUnicode_FromString(inp);
    PyTuple_SetItem(pArgs, 0, pValue1);
    PyObject* pResult = PyObject_CallObject(pFunc, pArgs);
    if (pResult == NULL)
        printf("Calling the python method failed.\n");
    string result = PyBytes_AS_STRING(pResult);
    cout << result << endl;
    Py_Finalize();
    return result;
}

int main()
{
    cout << myfunc("hello_world") << endl;
}

确切的错误是什么?欢迎使用堆栈溢出。请花点时间阅读并参考您可以在此处询问的内容和方式。它没有给出任何错误。它正在打印垃圾值。您需要在每行之后调用PyErr_occurrent(),并检查是否返回空指针。此外,如果pResult为null,您应该停止,然后不要尝试打印-这将产生垃圾结果。你的错误很可能是更早的
def func1(inp):
    return inp