cppyy
和ctypes
,因此这可能是一个愚蠢的问题。我有以下C ++函数:float method(const char* args[]) {
...
}
并且从Python我想将args
作为字符串列表传递,即:
args = *magic*
x = cppyy.gbl.method(args)
我以前找到了this,所以我用过
def setParameters(strParamList):
numParams = len(strParamList)
strArrayType = ct.c_char_p * numParams
strArray = strArrayType()
for i, param in enumerate(strParamList):
strArray[i] = param
lib.SetParams(numParams, strArray)
和来自Python:
args = setParameters([b'hello', b'world'])
c_types.c_char_p
需要一个字节数组。但是,当调用x = cppyy.gbl.method(args)
时,我得到
TypeError: could not convert argument 1 (could not convert argument to buffer or nullptr)
我不完全确定为什么这会出错,因为args
是<__main__.c_char_p_Array_2>
对象,我认为应该将其转换为const char* args[]
。
我最近才开始使用cppyy和ctypes,因此这可能是一个愚蠢的问题。我具有以下C ++函数:float method(const char * args []){...},从Python中,我想...
.cpp
文件:const char*
的ctypes数组的类型为const char*[2]
而不是const char*[]
,并且由于cppyy对ctypes类型执行直接类型匹配,因此失败。