忽略在swig-in中运算符<<的重新定义

2024-09-27 00:22:22 发布

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

我有两个版本的operator<<位于不同的命名空间中,它们具有相同的签名。由于swig将它们压缩到一个单独的名称空间中,它们彼此冲突,使我无法运行接口。在

我不需要使用脚本语言(Python)中的流插入运算符, 有没有办法抑制这些。在

%ignore指令似乎没有帮助。在

最小测试设置

头文件

//file:test.h
#include <iostream>
#include <boost/numeric/ublas/vector.hpp>

namespace probabilities{
    typedef boost::numeric::ublas::vector< double > UnivariateTable;

    inline 
    std::ostream& operator<<( std::ostream& ostr, const UnivariateTable& table){
       ostr<<"I am a table";
       return ostr;
    }


}

namespace positions{
    typedef boost::numeric::ublas::vector< double > PositionVector;
    inline 
    std::ostream& operator<<(std::ostream& ostr, const PositionVector& vect){
        ostr<<"I am a vector";
        return ostr;
    }
}

Swig接口文件

^{pr2}$

结果

[dmcnamara]$ swig -c++ -python -I/opt/vista_deps/include -I/opt/vista/include test.i 
test.h:26: Error: '__lshift__' is multiply defined in the generated target  language module in scope .
test.h:15: Error: Previous declaration of '__lshift__'

Tags: testinclude空间namespaceoperatorswigstddouble
2条回答

在写作过程中,我意识到:

您需要为%ignore指令指定名称空间:

%ignore positions::operator<<
%ignore probabilities::operator<<

也可以使用预处理工具:

//file:test.h
#include <iostream>
#include <boost/numeric/ublas/vector.hpp>

namespace probabilities{
    typedef boost::numeric::ublas::vector< double > UnivariateTable;
    #ifndef SWIG
    inline 
    std::ostream& operator<<( std::ostream& ostr, const UnivariateTable& table){
       ostr<<"I am a table";
       return ostr;
    }
    #endif


}

namespace positions{
    typedef boost::numeric::ublas::vector< double > PositionVector;
    #ifndef SWIG
    inline 
    std::ostream& operator<<(std::ostream& ostr, const PositionVector& vect){
        ostr<<"I am a vector";
        return ostr;
    }
    #endif
}

相关问题 更多 >

    热门问题