2012-04-05 93 views
1

我試圖重構我的項目的一部分,特別是Python/C++接口。 標準升壓::蟒蟒初始化是工作前:Boost :: Python Forward boost :: python :: object的聲明拋出python TypeError

boost::python::object main_module = boost::python::import("__main__"); 
boost::python::object globals(main_module.attr("__dict__")); 

// ...

然而,保理是爲它自己的階級之後,我越來越

TypeError: No to_python (by-value) converter found for C++ type: boost::python::api::proxy<boost::python::api::attribute_policies> 

當實例化對象PyInterface,如下:

namespace py = boost::python; 
class PyInterface 
{ 
private: 
    py::object 
     main_module, 
     global, 
     tmp; 
    //... 
public: 
    PyInterface(); 
    //... 
}; 

PyInterface::PyInterface() 
{ 
    std::cout << "Initializing..." << std::endl; 
    Py_Initialize(); 
    std::cout << "Accessing main module..." << std::endl; 
    main_module = py::import("__main__"); 
    std::cout << "Retrieve global namespace..." << std::endl; 
    global(main_module.attr("__dict__")); 
    //... 
} 

//in test.cpp 
int main() 
{ 
    PyInterface python; 
    //... 
} 

Running gives the following output: 
Initializing... 
Accessing main module... 
Retrieving global namespace... 

TypeError: No to_python (by-value) converter found for C++ type: boost::python::api::proxy<boost::python::api::attribute_policies> 

我能想到的唯一的事情就是它在使用它之前與聲明「全局變量」有關。在這種情況下,我有另一種方法可以做到這一點嗎?

回答

0

啊!修復。

在構造從

globals(main_method.attr("__dict__")); 

更改呼叫全局使用賦值運算符來代替:

globals = main_method.attr("__dict__"); 

回想起來,似乎十分明顯,但至少我知道我沒」只有一個人因爲沒有任何人對我進行欺騙而st jud不樂。