未定义的局部变量引用

时间:2017-03-05 20:57:07

标签: python c++ boost

我有C ++工厂类,应该在python脚本中使用:

class CCommandFactory {
    private:
        typedef std::map<std::string, IAbstractCommandCreator*>::iterator factory_iter;
        static std::map<std::string, IAbstractCommandCreator*> m_factory;
    public:
        template<class T>
        static void add(const std::string& id) {
            factory_iter it = m_factory.find(id);
            if (it == m_factory.end()) {
                m_factory[id] = new CCommandCreator<T>();
            }
        }
        static ICommand* create(const std::string& id, boost::python::list args) {
            factory_iter it = m_factory.find(id);
            if (it != m_factory.end()) {
                std::list<std::string> arguments;
                for (int i = 0; i < len(args); ++i) {
                    arguments.push_back(boost::python::extract<std::string>(args[i]));
                }
                return it->second->create(arguments);
            }
            return 0;
        }
        ~CCommandFactory() {
            for (factory_iter it = m_factory.begin(); it != m_factory.end(); ++it) {
                if (it->second) {
                    delete it->second;
                }
            }
        }
};  

这是包装器:

BOOST_PYTHON_MODULE(libfrtpsrv)
{
    class_<CCommandFactory, boost::noncopyable>("CCommandFactory", no_init)
        .def("create_command", &CCommandFactory::create, args("cmd_id", "args"), return_value_policy<manage_new_object>())
        .staticmethod("create_command")
    ;
}

代码对我来说是正确的,但是python脚本的执行在一开始就失败了:

Traceback (most recent call last):
  File example.py, line 1, in <module>
    import wrapper
  File .../wrapper.py, line 4, in <module>
    from libfrtpsrv import *
ImportError: .../daemon/libfrtpsrv.so: undefined symbol: _ZN15CCommandFactory9m_factoryE  

我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

您可以使用此命令查看哪个符号失败:

.collapsing

类的静态成员变量必须在某处定义,类中的声明只是声明,而不是定义。

解决方案是在CPP文件中的命名空间范围内添加$ c++filt <<< _ZN15CCommandFactory9m_factoryE CCommandFactory::m_factory 的定义(不是头文件,否则您将获得重复的符号):

m_factory