简体   繁体   中英

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. My class should be templated on the coordinate type (usually int/float/double etc.). 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 . I used different versions of G++ 4.5.2/4.7.2/4.8.1, but I still get the same error.

What am I missing here? Thanks in advance.

Using the free function boost::geometry::get<0>(p); recommended in the boost docs circumvents this problem.

I agree with the answer of us2012, using boost::geometry::get<0>() is recommended.

The actual problem was that the template keyword was missing, so this:

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

would have fixed the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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