2017-09-23 56 views
0

我是全新的使用swig,所以如果看起来很简单,请原谅我。Swig:接口简单的C++类,返回矢量

我想写一类的接口C++获取和返回的双重载体:

/* example.h file */ 
#include <iostream> 
#include <cstdio> 
#include <vector> 

class my_class 
{ 
    private:  
     int N; 
    public: 
     my_class(){ } 
     std::vector<double> half(const std::vector<double>&); 
}; 

/* example.cpp file */ 
#include "example.h" 

std::vector<double> my_class::half(const std::vector<double>& v) { 
    std::vector<double> w(v); 
    for (unsigned int i=0; i<w.size(); i++) 
     w[i] /= 2.0; 
    return w; 
} 

example.i接口文件

%module example 

%{ 
#define SWIG_FILE_WITH_INIT 
#include "example.h" 
%} 

%include "std_vector.i" 

namespace std { 
    %template(DoubleVector) vector<double>; 
} 


#include "example.h" 

然后我试图在python中使用模块:

import example 
A = example.my_class() 

AttributeError       Traceback (most recent call last) 
<ipython-input-5-d2af384d4adf> in <module>() 
----> 1 example.my_class() 

AttributeError: 'module' object has no attribute 'my_class' 

感谢您的任何指导或评论。

回答

0

example.i的最后一行应该是%include "example.h"而不是#include "example.h"