【问题标题】:"Syntax error in input" when SWIGging Boost.Geometry?SWIGging Boost.Geometry 时出现“输入语法错误”?
【发布时间】:2015-07-14 16:44:51
【问题描述】:

错误信息:

Error: Syntax error in input(1)

我的 Swig 文件:

%module interfaces

%{
#include <vector>
#include <list>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/linestring.hpp>
typedef boost::geometry::model::d2::point_xy<double> Point;
typedef boost::geometry::model::polygon<Point, true, false> Polygon;
%}

%include "std_vector.i"
%template(MultiPolygon) std::vector<Polygon>;
%template(pgon) Polygon;

如果我注释掉最后一行,它会编译

// %template(pgon) Polygon;

我一直在重新阅读有关模板的 swig 部分,但我完全不明白出了什么问题。我做错了什么,我该如何解决?

【问题讨论】:

  • 错误信息是什么...?
  • Polygon 不再是模板,而是具体类型,那么为什么需要%template(pgon)?如果您希望在 Python 世界中将 Polygon 称为 pgon,则 SWIG 可能有一些其他语法来创建别名。
  • @Praetorian Polygon 还不是模板类吗?在阅读 Swig 的文档时,我的印象是任何定义中带有 的类都需要包装在 %template()
  • @KarolyHorvath 错误:输入中的语法错误 (1)。
  • polygon 是一个类模板,polygon&lt;Point, true, false&gt; 或者它的别名,不再是一个模板,你已经通过提供所有模板参数来专门化它,它现在是一个真正的类型.

标签: python c++ boost swig boost-geometry


【解决方案1】:

即使Polygon 是一个类型定义或专门化的别名,您仍然需要将%template 与您关心的实际模板一起使用,例如:

%template(pgon) polygon<Point, true, false>;

您还需要向 SWIG 展示足够多的所涉及类型的定义/声明,以便它弄清楚发生了什么并使用正确的类型。

因此,按照您的意愿行事的最小的完整接口文件是:

%module poly

%{
#include <vector>
#include <list>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/linestring.hpp>
%}

%inline %{
typedef boost::geometry::model::d2::point_xy<double> Point;
typedef boost::geometry::model::polygon<Point, true, false> Polygon;
%}

namespace boost {
namespace geometry {
namespace model {
template<typename P, bool CW, bool CL> struct polygon {};
namespace d2 {
template <typename T> struct point_xy {};
}
}
}
}

%include "std_vector.i"
%template(Point) boost::geometry::model::d2::point_xy<double>;
%template(pgon) boost::geometry::model::polygon<Point, true, false>;
%template(MultiPolygon) std::vector<Polygon>;

这是因为 SWIG 需要知道它所包装的每种类型的定义以及 %template 指令。您还需要使您编写的 typedef 对 SWIG 和 C++ 编译器都可见,我使用 %inline 这样做以避免重复它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    相关资源
    最近更新 更多