我正在使用boost python。我导出了一些函数,该函数在参数中包含CL_DomElement
类。现在,当我运行应用程序时,我有:
TypeError: No to_python (by-value) converter found for C++ type: CL_DomElement
那么代码呢?我已经导出了函数,该函数在参数中使用了函数指针。这是代码:
typedef boost::function<boost::shared_ptr<Object> (CL_DomElement*, std::string& desc)> Parser;
void registerParser(std::string type, Parser p);
struct ParserProxy
{
bp::object callable;
ParserProxy(bp::object callable)
: callable(callable)
{ }
boost::shared_ptr<Object> operator()(CL_DomElement* elem, std::string& desc)
{
bp::object obj = callable(elem, desc);
return bp::extract<boost::shared_ptr<Object> >(obj);
}
};
void registerParserByProxy(std::string type, bp::object callable)
{
registerParser(type, ParserProxy(callable));
}
// In some boost.python module
bp::def("RegisterParser", registerParserByProxy);
我以这种方式注册(在python中):
class TestObj(Object):
@staticmethod
def ParseTestObj(node, desc):
print 'Parser is called!'
# Register parser
RegisterParser("testobj", TestObj.ParseTestObj)
它成功注册,我检查了我的 map (注册解析器将传递的键→值添加到std::map中),并且一切正常(添加了新值)。现在我要调用传递的指针:
boost::shared_ptr<Object> TypesManager::parseObject(CL_DomElement* objectTag, const std::string &type, std::string &desc)
{
return (getParser(type))(objectTag, desc);
}
getParser
从std::map返回带有键type
的功能指针。因此,据我了解,传递类
CL_DomElement
出了点问题。但是我在模块中做到了:bp::class_<CL_DomElement>("CL_DomElement");
我认为这不应防止我描述的此类错误。那怎么了
最佳答案
Boost.Python call by reference : TypeError: No to_python (by-value) converter found for C++ type: