我是 SIP 新手。我正在尝试为 C++ 文件创建一个简单的 python 包装器。如果您有 C++ 的示例 python 包装器,请帮助我。我有一个示例代码。但是当我运行命令时出现错误“sip: string.sip:72: This Python slot requires %MethodCode”:mkdir sipcode cp sipcode-Makefile sipcode/Makefile $(SIP) -s ".cc" -c sipcode字符串.sip
string.sip 的代码如下所述:
%模块字符串
命名空间标准{
类字符串 {
%TypeHeaderCode
包括字符串.h
%结尾
公共:字符串();
string(const char*);
string(const std::string&);
bool empty();
int length();
int size();
void resize(int, char);
void resize(int);
int capacity();
void reserve(int =0);
std::string& append(const std::string&);
std::string& append(const std::string&, int, int);
std::string& append(const char*);
std::string& append(const char*, int);
std::string& append(int, char);
std::string& insert(int, const std::string&);
std::string& insert(int, const std::string&, int, int);
std::string& insert(int, const char*);
std::string& insert(int, const char*, int);
std::string& insert(int, int, char);
int find(const std::string&, int = 0);
int find(const char*, int, int);
int find(const char*, int = 0);
int find(char, int = 0);
int rfind(const std::string&, int = -1);
int rfind(const char*, int, int);
int rfind(const char*, int = -1);
int rfind(char, int = -1);
int find_first_of(const std::string&, int = 0);
int find_first_of(const char*, int, int);
int find_first_of(const char*, int = 0);
int find_first_of(char, int = 0);
int find_first_not_of(const std::string&, int = 0);
int find_first_not_of(const char*, int, int);
int find_first_not_of(const char*, int = 0);
int find_first_not_of(char, int = 0);
int find_last_of(const std::string&, int = 0);
int find_last_of(const char*, int, int);
int find_last_of(const char*, int = 0);
int find_last_of(char, int = 0);
int find_last_not_of(const std::string&, int = 0);
int find_last_not_of(const char*, int, int);
int find_last_not_of(const char*, int = 0);
int find_last_not_of(char, int = 0);
std::string substr(int = 0, int = -1);
无效的str();%MemberCode const char* s; std::string* ptr;
if (sipParseArgs(&sipArgsParsed,sipArgs,"J1",sipClass_std_string, &ptr))
{
s = ptr->c_str();
/* Python API reference, P40 */
return PyString_FromString(s);
}
%结尾
无效的getitem();%MemberCode std::string* ptr; 诠释我;
if (sipParseArgs(&sipArgsParsed,sipArgs,"J1i",sipClass_std_string, &ptr, &i))
{
if (i >= ptr->length())
{
/* Python API Reference, Chapter 4 */
PyErr_SetString ( PyExc_IndexError ,"string index out of range" );
return NULL;
}
else
return Py_BuildValue("c", ptr->at(i));
}
%结尾
}; };