_swig_getattr 属性错误

2 投票
1 回答
1635 浏览
提问于 2025-04-16 07:34

我在用swig把一个C语言函数封装成可以在Python代码中使用的时候,遇到了一个属性错误。我有其他的函数和chap一起用,运行得很好,但不知道为什么这个函数就是不行 :/

我想用CGAL来找出所有粒子的凸包(chap)。下面是chap函数和错误追踪信息:

std::vector<Vec2d> World::chap() const
{
    std::vector<Point_2> ch;
    std::vector<Point_2> points;
    std::vector<Vec2d> result;
    for(size_t i = 0, sz = particles.size(); i < sz; ++i)
    {
        points.push_back(Point_2(particles[i]->position.x, particles[i]->position.y));
    }
    CGAL::convex_hull_2(points.begin(), points.end(), std::back_inserter(ch));
    for(size_t i = 0, sz = ch.size(); i < sz; ++i)
    {
        result.push_back(Vec2d(ch[i].x(), ch[i].y()));
    }
    return result;
}

Traceback (most recent call last):
  File "keiro.py", line 64, in <module>
    if scenario.run():
  File "/Users/marcstrauss/Desktop/keiro/scenario.py", line 56, in run
    dt = self.world.advance()
  File "/Users/marcstrauss/Desktop/keiro/world.py", line 87, in advance
    view.add_ch(self.chap())
  File "/Users/marcstrauss/Desktop/keiro/fast/physics.py", line 312, in <lambda>
    __getattr__ = lambda self, name: _swig_getattr(self, World, name)
  File "/Users/marcstrauss/Desktop/keiro/fast/physics.py", line 34, in _swig_getattr
    raise AttributeError,name
AttributeError: chap

1 个回答

2

这个函数是公开的吗?我在尝试调用一个声明为“受保护”的函数时也遇到了同样的错误。SWIG C++ 手册提到,私有和受保护的函数是不会被包装的。

撰写回答