将C++ EnUM类暴露到Python COD中的错误

2024-07-04 08:00:56 发布

您现在位置:Python中文网/ 问答频道 /正文

<>我必须将C++ EnUM类暴露到Python中。 我检查了一些示例,但都是关于C样式enum类型的。但是,我需要使用enum类。有什么建议吗?在

这是我的代码:

zoo.h


namespace extzoo
{
namespace intzoo
{
class zoo
{
public:
enum class Size
{
small, medium, large
};
const std::string hello_zoo();
const std::string getname_zoo();
const Size get_size();
void set_size(Size);
private:
Size size;
};
}
}

zoo.cpp


^{pr2}$

将C++方法暴露到Python

的代码

pyintf.cpp


^{3}$

我有以下错误:

[root@localhost enumclass]# g++ -shared -std=c++11 -o pyintf.so -fPIC pyintf.cpp zoo.h zoo.cpp -lboost_python -lpython2.7 -I/usr/include/python2.7
pyintf.cpp: In function ‘void init_module_pyintf()’:
pyintf.cpp:34:2: error: ‘extzoo::intzoo::python’ has not been declared
python::enum_<zoo::Size>("Size")
pyintf.cpp:34:25: error: expected primary-expression before ‘>’ token
python::enum_<zoo::Size>("Size")
pyintf.cpp:35:8: error: request for member ‘value’ in ‘("Size")’, which is of non-class type ‘const char [5]’
.value("small", zoo::small)
pyintf.cpp:35:23: error: ‘small’ is not a member of ‘extzoo::intzoo::zoo’
.value("small", zoo::small)
pyintf.cpp:36:24: error: ‘medium’ is not a member of ‘extzoo::intzoo::zoo’
.value("medium", zoo::medium)
pyintf.cpp:37:19: error: ‘large’ is not a member of ‘extzoo::intzoo::zoo’
.value("large", zoo::large)

Tags: sizevaluenoterrorenumcppsmallmember
1条回答
网友
1楼 · 发布于 2024-07-04 08:00:56

与前面的SO question中相同,问题是类方法定义不是在名称空间中定义的。使用using namespace ...是为了获得该命名空间内容的可见性,但不是为了定义该命名空间的内容。在

相关问题 更多 >

    热门问题