2012-11-09 2 views
1

나는 C++ 클래스를 가지고 있으므로 파이썬 코드에서 사용해야합니다. 이를 위해 swig를 사용하여 래퍼 클래스를 생성합니다. 문서에 따라, 본보기swig 빌드 문제 - python

/* File: example.i */ 
%module example 

%{ 
#define SWIG_FILE_WITH_INIT 
#include "item.h" 
#include "GradedComplex.h" 
#include "GradedDouble.h" 
%} 

%include "item.h" 
%include "GradedComplex.h" 
%include "GradedDouble.h" 

를 구성 그리고 나는 다음과 같은 오류를 얻고 두 번째 명령을 실행 한 후 창

c:>swig -c++ -python example.i 

c:>python setup.py build_ext --inplace 

에 꿀꺽 꿀꺽를 사용하여 래퍼 클래스를 구축을 위해 노력하고 있습니다.

example_wrap.cxx 
C:\Program Files\Microsoft Visual Studio 9.0\VC\INCLUDE\xlocale(342) : warning C 
4530: C++ exception handler used, but unwind semantics are not enabled. Specify 
/EHsc 
c:\documents and settings\swig\item.h(73) : warning C45 
21: 'Item<std::complex<double>>' : multiple copy constructors specified 
c:\documents and settings\swig\item.h(57) : error C2106 
: '+=' : left operand must be l-value 
c:\documents and settings\swig\item.h(58) : error C2106 
: '+=' : left operand must be l-value 
c:\documents and settings\swig\item.h(63) : error C2106 
: '-=' : left operand must be l-value 
c:\documents and settings\swig\item.h(64) : error C2106 
: '-=' : left operand must be l-value 
c:\documents and settings\swig\item.h(69) : error C2106 
: '=' : left operand must be l-value 
c:\documents and settings\swig\item.h(70) : error C2106 
: '=' : left operand must be l-value 
example_wrap.cxx(3275) : error C2512: 'Item<std::complex<double>>' : no appropriate default constructor available 

c:\documents and settings\swig\Item.h(38) : warning C45 
21: 'Item<T>' : multiple copy constructors specified 
     with 
     [ 
      T=double 
     ] 
     example_wrap.cxx(3425) : see reference to class template instantiation ' 
Item<T>' being compiled 
     with 
     [ 
      T=double 
     ] 
error: command '"C:\Program Files\Microsoft Visual Studio 9.0\VC\BIN\cl.exe"' fa 
iled with exit status 2 

item.h 당신은 C++ 코드에서 오류가 다음

#ifndef __ITEM_H__ 
#define __ITEM_H__ 

#include <complex> 
#include <functional> 
#include <string> 

template<typename T> 
class Item 
{ 
    std::string name_; 
    T val_; 

public: 
    Item(std::string name, T val) : name_(name), val_(val) {} 
    Item(Item<T> &rhs) : name_(rhs.name_), val_(rhs.val_) {} 
    Item(const Item<T> &rhs) : name_(rhs.name_), val_(rhs.val_) {} 
    ~Item() {} 

    std::string name() const { return name_; } 
    T operator()() const { return val_; } 
    double norm() const { return sqrt(val_ * val_); } 
    Item<T> &operator+=(Item<T> &rhs) 
    { 
    val_ += rhs(); 
    return *this; 
    } 
    Item<T> &operator-=(Item<T> &rhs) 
    { 
    val_ -= rhs(); 
    return *this; 
    } 
    Item<T> &operator*=(Item<T> &rhs) 
    { 
    val_ *= rhs(); 
    return *this; 
    } 
}; 

template<> 
class Item<std::complex<double> > 
{ 
    std::string name_; 
    std::complex<double> val_; 

public: 
    Item(std::string name, std::complex<double> val) : name_(name), val_(val) {} 
    Item(Item<std::complex<double> > &rhs) : name_(rhs.name_), val_(rhs.val_) {} 
    Item(const Item<std::complex<double> > &rhs) : name_(rhs.name_), val_(rhs.val_) {} 
    ~Item() {} 

    std::string name() const { return name_; } 
    std::complex<double> operator()() const { return val_; } 
    double norm() const { return sqrt(val_.real() * val_.real() + val_.imag() * val_.imag()); } 
    Item<std::complex<double> > &operator+=(Item<std::complex<double> > &rhs) 
    { 
    val_.real() += rhs().real(); 
    val_.imag() += rhs().imag(); 
    return *this; 
    } 
    Item<std::complex<double> > &operator-=(Item<std::complex<double> > &rhs) 
    { 
    val_.real() -= rhs().real(); 
    val_.imag() -= rhs().imag(); 
    return *this; 
    } 
    Item<std::complex<double> > &operator*=(Item<std::complex<double> > &rhs) 
    { 
    val_.real() = val_.real() * rhs().real() - val_.imag() * rhs().imag(); 
    val_.imag() = val_.real() * rhs().imag() + val_.imag() * rhs().real(); 
    return *this; 
    } 
}; 

template<typename T> 
struct ItemComparator : public std::binary_function<Item<T>, Item<T>, bool> 
{ 
    inline bool operator()(Item<T> lhs, Item<T> rhs) 
    { 
    return lhs.norm() < rhs.norm(); 
    } 
}; 

#endif 

답변

1

입니다. std::complex::real()는 좌변 값을 반환하지 않는, 그래서 이것은 컴파일되지 않습니다 : 귀하의 회신

val_ += rhs(); 
+0

감사 :

Item<std::complex<double> > &operator+=(Item<std::complex<double> > &rhs) { val_.real() += rhs().real(); val_.imag() += rhs().imag(); return *this; } 

당신은 그냥이 사용할 수 –