在坐标类型和使用增强几何库模板化我的类时出现编译错误

Compilation error when templating my class on coordinate type and using Boost Geometry library

本文关键字:我的 错误 编译 何库模 类型 坐标 增强      更新时间:2023-10-16

我正在 Boost Geometry 库之上编写一个库代码。我的类应该在坐标类型(通常是 int/float/double 等)上模板化。下面的代码(精简到最低限度)无法编译,我得到一个对我没有帮助的编译错误。

代码:

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point.hpp>
template <typename CoordType>
class MyClass {
public:
    typedef boost::geometry::model::point<CoordType, 2, boost::geometry::cs::cartesian> MyPoint;
    CoordType getX(const MyClass<CoordType>::MyPoint &p) const { return p.get<0>(); }
};

错误:

test.cpp: In member function 'CoordType MyClass<CoordType>::getX(const MyClass<CoordType>::MyPoint&) const':
test.cpp:8:82: error: expected primary-expression before ')' token

我正在编译此代码:g++ -I./boost_1_54_0 test.cpp -o test.o .我使用了不同版本的 G++ 4.5.2/4.7.2/4.8.1,但仍然收到相同的错误。

我在这里错过了什么?提前谢谢。

使用提升文档中推荐boost::geometry::get<0>(p);的免费函数可以避免这个问题。

我同意 us2012 的答案,建议使用 boost::geometry::get<0>()。

实际问题是缺少模板关键字,因此:

{ return p.template get<0>(); }

会解决问题。