【发布时间】:2017-03-30 09:10:58
【问题描述】:
我将使用 swig 制作 python 包装器。 我在 c++ dll 中有一些类和类型。
class Image
{
public:
Image(const Image&);
Image& operator= (const Image&);
~Image();
unsigned int width() const;
unsigned int height() const;
};
struct Property
{
std::string path;
std::string datetime;
};
typedef std::pair<Image, Property> MyImage;
class Manage
{
public:
Manage();
~Manage();
void addMyImage(MyImage img);
MyImage getMyImage(int index);
};
我在这个之后制作了 swig 接口文件:
%include "std_pair.i"
%template(MyImage) std::pair<Image, Property>;
class Image
{
public:
Image(const Image&);
~Image();
unsigned int width() const;
unsigned int height() const;
};
class Manage
{
public:
Manage();
~Manage();
void addMyImage(std::pair<Image, Property> img);
std::pair<Image, Property> getMyImage(int index);
};
我运行命令swig -c++ -python Test.swig。
我在 Visual Studio 中编译 Test_wrapper.cxx,出现以下错误:
error C2512: 'Image' : 没有合适的默认构造函数可用
所以我尝试使用swig -c++ -python -nodefaultctor Test.swig。
但它是一样的。
============更新============
问题是std::pair<Image, Property>。对创建时,它调用参数的构造函数。图像没有默认构造函数。所以会发生错误。
我该如何解决? 谢谢。
【问题讨论】:
-
我被难住了。我理解为什么我认为 nodefaultctor 不起作用(有一个明确的,所以它不是默认的)。我找不到任何可行的解决方法。 stackoverflow.com/questions/13569939/… 看起来也很相关。当您显示的代码足以复制和粘贴时,值得提出的问题更容易回答 - 我必须在 C++ 标头中添加
#include <utility>并在 SWIG 界面中添加%module %{ #include "test.hh" %}以使它们尽可能远你的错误。