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
C++ 如何使用PyObject\u CallObject查询复杂返回类型的数据?_C++_Python 3.x_Python C Api - Fatal编程技术网

C++ 如何使用PyObject\u CallObject查询复杂返回类型的数据?

C++ 如何使用PyObject\u CallObject查询复杂返回类型的数据?,c++,python-3.x,python-c-api,C++,Python 3.x,Python C Api,我有一个小型python程序,可以计算2d位置 def getPosition(lerpParameter): A = getClothoidConstant() ... python脚本可以在我的项目文件夹中找到 文件中包含函数getPosition。该函数返回Vector2类型的值。如何在C++程序中访问返回向量2D的数据?< /P> < C++程序看起来是这样的: /// Get function PyObject *pFunc = PyObject_G

我有一个小型python程序,可以计算2d位置

def getPosition(lerpParameter):
    A = getClothoidConstant()
    ...
python脚本可以在我的项目文件夹中找到

文件中包含函数getPosition。该函数返回Vector2类型的值。如何在C++程序中访问返回向量2D的数据?< /P> < C++程序看起来是这样的:

    /// Get function
    PyObject  *pFunc = PyObject_GetAttrString(pModule, pid.functionName);
    // pFunc is a new reference

    if (pFunc && PyCallable_Check(pFunc)) 
    {
        PyObject *pArgs = PyTuple_New(pid.getArgumentCount());

        PyObject *pValue = nullptr;

        // setup function parameters
        for (int i = 0; i < pid.getArgumentCount(); ++i) 
        {
            if (pid.arguments[i].type == eType::Int)
            {
                pValue = PyLong_FromLong(atoi(pid.arguments[i].name.c_str()));
            }
            else
            {
                pValue = PyFloat_FromDouble(atof(pid.arguments[i].name.c_str()));
            }

            if (!pValue) 
            {
                Py_DECREF(pArgs);
                Py_DECREF(pModule);
                std::cout << "Cannot convert argument" << std::endl;
                throw std::runtime_error("Cannot convert argument");
            }
            // pValue reference stolen here:
            PyTuple_SetItem(pArgs, i, pValue);
        }

        pValue = PyObject_CallObject(pFunc, pArgs);
        Py_DECREF(pArgs);

        if (pValue != nullptr)
        {
            switch (pid.returnType)
            {
...

            case eType::Double:
                //std::cout << "Result of call: " << PyFloat_AsDouble(pValue) << std::endl;
                doubleValue_ = PyFloat_AsDouble(pValue);
                break;

            case  eType::Vector2d:
                {
                    // How can I acccess the data here?

                }
                break;

            default:
                break;
            }

            Py_DECREF(pValue);
        }
///Get函数
PyObject*pFunc=PyObject_GetAttrString(pModule,pid.functionName);
//pFunc是一个新的参考
if(pFunc&&PyCallable_检查(pFunc))
{
PyObject*pArgs=PyTuple_New(pid.getArgumentCount());
PyObject*pValue=nullptr;
//设置功能参数
对于(int i=0;istd::cout如果您想要获得“x”属性,并且您知道它是一个浮点:

PyObject *temp = PyObject_GetAttrString(pValue, "x");
if (temp == NULL) {
    // error handling
}
double x = PyFloat_AsDouble(temp);

// clean up reference when done with temp
Py_DECREF(temp);
(pValue,“x”)
返回对
x
属性的新引用。