我正在出口两类动物园(extzoo和intzoo) 名称空间),类动物(extanim和intanim名称空间)方法 在两级嵌套命名空间中定义。我想公开这些方法 到Python解释器,从那里我可以访问它们。我写过代码, 创建了一个共享库,但是当我将它导入python时,我得到了一个错误。 我将感谢你的指导。你知道吗
在类似的情况下,我遵循了以下链接给出的答案 暴露C++嵌套命名空间:create boost-python nested namespace
动物园.h:
namespace extzoo
{
namespace intzoo
{
class zoo
{
public:
const std::string hello_zoo();
const std::string getname_zoo();
};
}
}
你知道吗动物园.cpp地址:
using namespace extzoo;
using namespace intzoo;
const std::string zoo::hello_zoo() {
return std::string("hello, zoo");
}
const std::string zoo::getname_zoo() {
std::string input;
std::cout<<"Please enter your favorit zoo name: ";
std::getline(std::cin,input);
return std::string("Your favorit zoo name is: ").append(input);
}
动物h:
namespace extanim
{
namespace intanim
{
class animal
{
public:
const std::string hello_animal();
const std::string getname_animal();
};
}
}
你知道吗动物.cpp地址:
using namespace extanim;
using namespace intanim;
const std::string animal::hello_animal() {
return std::string("hello, animal");
}
const std::string animal::getname_animal() {
std::string input;
std::cout<<"Please enter your favorit animal name: ";
std::getline(std::cin,input);
return std::string("Your favorit animal name is: ").append(input);
}
你知道吗pyintf.cpp公司地址:
// An established convention for using boost.python.
using namespace boost::python;
//user defined ns
using namespace extzoo::intzoo;
using namespace extzoo;
using namespace extanim::intanim;
using namespace extanim;
class DummyZoo{};
class DummyAnimal{};
BOOST_PYTHON_MODULE(pyintf) {
scope intzoo
= class_<DummyZoo>("intzoo");
class_<extzoo::intzoo::zoo>("zoo")
// Expose the function hello_zoo().
.def("hello_zoo", &extzoo::intzoo::zoo::hello_zoo)
// Expose the function getname_zoo().
.def("getname_zoo", &extzoo::intzoo::zoo::getname_zoo)
;
scope intanim
= class_<DummyAnimal>("intanim");
class_<extanim::intanim::animal>("animal")
// Expose the function hello_animal().
.def("hello_animal", &extanim::intanim::animal::hello_animal)
// Expose the function getname_animal().
.def("getname_animal", &extanim::intanim::animal::getname_animal)
;
}
我使用以下命令编译了上述代码:
g++ -shared -o pyintf.so -fPIC pyintf.cpp zoo.h zoo.cpp animal.h animal.cpp -lboost_python -lpython2.7 -I/usr/include/python2.7
导入共享库时出错
import pyintf Traceback (most recent call last): File "", line 1, in ImportError: ./pyintf.so: undefined symbol: _ZN7extanim7intanim6animal14getname_animalEv
+Update+Update+Update+Update+Update+Update+Update+Update+Update+Update+Update+Update+Update+Update+Update+Update+Update+Update+Update+Update+Update+Update+Update+Update+Update+Update+Update+Update
我已经根据@doqtor建议更改了代码。但是,现在我无法在Python中导入在一个文件中定义的名称空间“intanim”,而另一个文件可以导入“intzoo”。正如你在下面看到的。你知道吗
>>> import pyintf
>>> pyintf.intanim.animal().hello_animal()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'intanim'
>>> from pyintf import intanim
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name intanim
>>> from pyintf import extanim
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name extanim
>>> from pyintf import intzoo
>>> intzoo.zoo().hello_zoo()
'hello, zoo'
在cpp文件中,需要将类方法的定义放在适当的名称空间中,而不是
using namespace ...
声明。例如动物园.cpp这将是:-更新
使用
{}
在c++代码中添加作用域以分隔python作用域的定义:以及测试:
请注意,导入
extanim
时会出现错误,因为没有定义此类名称的作用域。你知道吗相关问题 更多 >
编程相关推荐