1

我只是写了一个 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>'
4

1 回答 1

5

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

using namespace std;
using namespace boost::geometry;

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

如果您删除using namespace std;,并在必要时符合std::条件,那么一切都应该没问题。或者,如果你真的想保持命名空间污染,那么写限定名,boost::geometry::distance.

于 2012-12-11T12:47:11.273 回答