使用Boost.Geometry时“二进制表达式的无效操作数”?

Posted

技术标签:

【中文标题】使用Boost.Geometry时“二进制表达式的无效操作数”?【英文标题】:"invalid operands to binary expression" when using Boost.Geometry? 【发布时间】:2011-10-11 03:30:14 【问题描述】:

当我尝试提升几何差分函数时,我得到一个很长的编译器错误,而具有相同接口和可能相关实现的并集和交集工作:

bg::unique_(OldPolygon, Node->Polygon, NodePolygon); // compiles
bg::intersection(OldPolygon, Node->Polygon, NodePolygon); // compiles
bg::difference(OldPolygon, Node->Polygon, NodePolygon); // dies

第一个错误是:

boost/range/size.hpp:32:13: error: invalid operands to
    binary expression ('
         boost::reverse_iterator<
             __gnu_cxx::__normal_iterator<
                 const Graphpoint *,
                 std::vector<
                     GraphPoint,
                     std::allocator<GraphPoint>
                 >
             >
         >' and 'int')
            BOOST_ASSERT( (boost::end(rng) - boost::begin(rng)) >= 0 &&
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

似乎由于某种原因,迭代器的区别是返回反向迭代器而不是距离......

类型声明为:

namespace bg = boost::geometry;

struct GraphPoint

    int x, y;
    GraphPoint(int x, int y) : x(x), y(y)  
    GraphPoint() : x(0), y(0)  
    GraphPoint(const GraphPoint &other) : x(other.x), y(other.y)  

    bool operator ==(const GraphPoint &other) const
    
        return x == other.x && y == other.y;
    
;

BOOST_GEOMETRY_REGISTER_POINT_2D(GraphPoint, int, bg::cs::cartesian, x, y)

typedef bg::model::polygon<GraphPoint> Polygon;
typedef Polygon::ring_type Ring;
typedef bg::model::multi_polygon<Polygon> MultiPolygon;

MultiPolygon OldPolygon;
struct Node

    Polygon Polygon;

MultiPolygon NodePolygon;

完整的错误在here 以防有人喜欢挖掘。 如何编译?

【问题讨论】:

【参考方案1】:

我根据您的示例成功编译了一个示例:

VS2005 SP1 (Vista x64) boost 1.48.0(刚刚下载)

我不得不修改 Node 结构,这是生成的代码:

#include <boost/geometry/geometry.hpp> 
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/geometry/geometries/register/ring.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/multi/geometries/multi_polygon.hpp> // boost 1_48_0
//#include <boost/geometry/multi/geometries/multi_geometries.hpp> // if boost comes from SVN

namespace bg = boost::geometry;

struct GraphPoint

    int x, y;
    GraphPoint(int x, int y) : x(x), y(y)  
    GraphPoint() : x(0), y(0)  
    GraphPoint(const GraphPoint &other) : x(other.x), y(other.y)  

    bool operator ==(const GraphPoint &other) const
    
        return x == other.x && y == other.y;
    
;

BOOST_GEOMETRY_REGISTER_POINT_2D(GraphPoint, int, bg::cs::cartesian, x, y)

typedef bg::model::polygon<GraphPoint> Polygon;
typedef Polygon::ring_type Ring;
typedef bg::model::multi_polygon<Polygon> MultiPolygon;

MultiPolygon OldPolygon;
MultiPolygon NodePolygon;

struct Node

    Polygon p;
 node;

int main(int argc, char* argv[])

    bg::unique(OldPolygon); // only one parameter
    bg::intersection(OldPolygon, node.p, NodePolygon);
    bg::difference(OldPolygon, node.p, NodePolygon);

    return 0;

我收到两个警告 (warning C4244: '=' : conversion from 'double' to 'int', possible loss of data),但它可以编译。

【讨论】:

以上是关于使用Boost.Geometry时“二进制表达式的无效操作数”?的主要内容,如果未能解决你的问题,请参考以下文章

使用 boost::geometry::buffer 函数

在将元素插入 boost::geometry::index::rtree 时获取“可索引无效”

如何包含Boost库?

我可以在线程中使用 Boost.Geometry.index.rtree 吗?

奇怪的行为将 boost::geometry::within() 用于多边形和环

未能使用boost :: geometry :: model :: polygon