문제

I have two versions of operator<< in separate namespaces that have the same signature. Since swig squashes them into a single name-space they conflict with one another, preventing me from running the interface.

I do not need to use the stream-insertion operator from the scripting language (Python), is there a way to suppress these.

The %ignore directive doesn't seem to help.

A minimal test setup

Header File

//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 interface file

//file:test.i
%module test

%{
#include "test.h"
%}

%ignore operator<<;
%include "test.h"

Result

[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__'
도움이 되었습니까?

해결책

In the process of writing the question, I realized the answer:

you need to specify the namespaces for the %ignore directives:

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

다른 팁

You can also use the preprocessing tools :

//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
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top