简体   繁体   English

在坐标类型上模拟我的类并使用Boost Geometry库时出现编译错误

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

I'm writing a library code on top of Boost Geometry library. 我正在Boost Geometry库上编写库代码。 My class should be templated on the coordinate type (usually int/float/double etc.). 我的类应该在坐标类型上进行模板化(通常是int / float / double等)。 The code below (stripped down to bare minimum) doesn't compile and I get a compilation error that doesn't help me. 下面的代码(剥离到最低限度)不编译,我得到一个对我没有帮助的编译错误。

The code: 编码:

#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>(); }
};

The error: 错误:

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

I'm compiling this code with: g++ -I./boost_1_54_0 test.cpp -o test.o . 我正在使用以下代码编译此代码: g++ -I./boost_1_54_0 test.cpp -o test.o I used different versions of G++ 4.5.2/4.7.2/4.8.1, but I still get the same error. 我使用了不同版本的G ++ 4.5.2 / 4.7.2 / 4.8.1,但我仍然得到同样的错误。

What am I missing here? 我在这里错过了什么? Thanks in advance. 提前致谢。

Using the free function boost::geometry::get<0>(p); 使用自由函数boost::geometry::get<0>(p); recommended in the boost docs circumvents this problem. 在boost文档中推荐可以避免这个问题。

I agree with the answer of us2012, using boost::geometry::get<0>() is recommended. 我同意us2012的答案,建议使用boost :: geometry :: get <0>()。

The actual problem was that the template keyword was missing, so this: 实际问题是缺少模板关键字,因此:

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

would have fixed the problem. 会解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM