【问题标题】:boost geometry distance() error in compile编译时增加几何距离()错误
【发布时间】:2012-12-11 12:26:40
【问题描述】:

我只是写了一个boost(http://www.boost.org/doc/libs/1_52_0/libs/geometry/doc/html/geometry/quickstart.html)给出的简单例子。编译过程中出现一些错误。我使用 eclipse 和 Mingw 来编译它。谁能告诉我有什么问题?

测试代码如下:

#include <iostream>
using namespace std;

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/algorithms/distance.hpp>

using namespace boost::geometry;

int main() {
cout << "!!!Hello World!!!" << endl; 
model::d2::point_xy<int> p1(1, 1), p2(2, 2);
cout << "Distance p1-p2 is: " << distance(p1, p2) << endl;
return 0;
}

错误如下:

c:\program files\mingw64\bin\../lib/gcc/x86_64-w64-  
mingw32/4.7.1/include/c++/bits/stl_iterator_base_funcs.h:114:5:   
required by substitution of 'template<class _InputIterator> 
typename std::iterator_traits::difference_type 
std::distance(_InputIterator, _InputIterator) [with _InputIterator 
= boost::geometry::model::d2::point_xy<int>]'
..\src\test.cpp:22:50:   required from here
c:\program files\mingw64\bin\../lib/gcc/x86_64-w64-  
mingw32/4.7.1/include/c++/bits/stl_iterator_base_types.h:166:53: 
error: no type named 'iterator_category' in 'class 
boost::geometry::model::d2::point_xy<int>'
c:\program files\mingw64\bin\../lib/gcc/x86_64-w64-
mingw32/4.7.1/include/c++/bits/stl_iterator_base_types.h:167:53: 
error: no type named 'value_type' in 'class   
boost::geometry::model::d2::point_xy<int>'
c:\program files\mingw64\bin\../lib/gcc/x86_64-w64- 
mingw32/4.7.1/include/c++/bits/stl_iterator_base_types.h:168:53: 
error: no type named 'difference_type' in 'class  
boost::geometry::model::d2::point_xy<int>'
c:\program files\mingw64\bin\../lib/gcc/x86_64-w64- 
mingw32/4.7.1/include/c++/bits/stl_iterator_base_types.h:169:53: 
error: no type named 'pointer' in 'class 
boost::geometry::model::d2::point_xy<int>'
c:\program files\mingw64\bin\../lib/gcc/x86_64-w64-  
mingw32/4.7.1/include/c++/bits/stl_iterator_base_types.h:170:53: 
error: no type named 'reference' in 'class 
boost::geometry::model::d2::point_xy<int>'

【问题讨论】:

  • 这在 gcc-4.3.2 和今天的 trunk 中编译得很好,你使用了什么编译器标志?
  • 我使用 gcc-4.7.1。你觉得这个问题是gcc的版本引起的吗?
  • 不知道,但可能是。您确定您的 Boost 版本足够新以支持您的编译器吗?
  • 我用的是最新版本的 boost 1.52.0。我怎么知道它是否足够新?
  • 感谢您的提醒。看来 boost 支持 gcc-4.7.0。但它没有说 gcc-4.7.1。

标签: c++ boost compilation boost-geometry


【解决方案1】:

这就是为什么你应该避免使用 using 指令。你有:

using namespace std;
using namespace boost::geometry;

将这些命名空间中的所有名称拖到全局命名空间中。这包括 std::distanceboost::geometry::distance 并且(从错误消息判断)std::distance 被选为更好的重载。

如果您删除using namespace std;,并在必要时使用std:: 进行限定,那么一切都会好起来的。或者,如果你真的想保持命名空间污染,那就写限定名,boost::geometry::distance

【讨论】:

  • 是的,你是对的!当我在距离前面添加“boost::geometry::”时。有用。你能告诉我如何避免这个错误吗?我不想一直在每个函数中添加这么长的字符。非常感谢!
  • @user1894177:最好的方法是不使用 using 指令,或者至少将它们限制在比整个文件更窄的范围内。就我个人而言,我从不使用using namespace std;(因为std:: 是一个如此短的前缀,不值得去掉);对于更长的命名空间,您可以给它一个短别名,例如namespace geo = boost::geometry.
猜你喜欢
  • 2018-02-19
  • 1970-01-01
  • 2014-11-06
  • 1970-01-01
  • 2016-05-11
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多