【问题标题】:Swig: interface simple C++ class that returns vectorSwig:返回向量的接口简单 C++ 类
【发布时间】:2017-09-23 07:41:08
【问题描述】:

我在使用 swig 方面是全新的,如果看起来很简单,请原谅我。

我想为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'

感谢任何指导或评论。

【问题讨论】:

    标签: python c++ swig


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-06
      • 2013-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多