C++ 编译时出现boost geometry distance()错误
我只写了一个boost()给出的简单示例。编译过程中出现了一些错误。我使用eclipse和Mingw来编译它。谁能告诉我有什么问题吗 测试代码如下所示:C++ 编译时出现boost geometry distance()错误,c++,boost,compilation,boost-geometry,C++,Boost,Compilation,Boost Geometry,我只写了一个boost()给出的简单示例。编译过程中出现了一些错误。我使用eclipse和Mingw来编译它。谁能告诉我有什么问题吗 测试代码如下所示: #include <iostream> using namespace std; #include <boost/geometry.hpp> #include <boost/geometry/geometries/point_xy.hpp> #include <boost/geometry/geome
#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>'
#包括
使用名称空间std;
#包括
#包括
#包括
#包括
使用名称空间boost::geometry;
int main(){
cout这就是为什么你应该避免使用指令。你有:
using namespace std;
using namespace boost::geometry;
将这些名称空间中的所有名称拖动到全局名称空间中。其中包括std::distance
和boost::geometry::distance
,并且(从错误消息判断)std::distance
被选为更好的重载
如果您使用命名空间std;
删除,并在必要时使用std::
进行限定,则一切都应该正常。或者,如果您确实希望保持命名空间污染,请编写限定名称,boost::geometry::distance
,这就是您应该避免使用指令的原因。您有:
using namespace std;
using namespace boost::geometry;
将这些名称空间中的所有名称拖动到全局名称空间中。其中包括std::distance
和boost::geometry::distance
,并且(从错误消息判断)std::distance
被选为更好的重载
如果您使用名称空间std;
删除,并在必要时使用std:
进行限定,那么一切都应该很好。或者,如果您确实想保持名称空间的污染,那么请编写限定名称,boost::geometry::distance
这在gcc-4.3.2和今天的trunk中编译得很好,什么编译器你使用了什么标志?我使用的是gcc-4.7.1。你认为这个问题是由gcc版本引起的吗?不知道,但可能是。你确定你的Boost版本足够新,可以支持你的编译器吗?我使用的是Boost 1.52.0的最新版本。我如何知道它是否足够新?谢谢你的提醒。看起来Boost支持gcc-4.7.0。但它没有说明gcc-4.7.1。这在gcc-4.3.2和今天的主干中编译良好,您使用了什么编译器标志?我使用gcc-4.7.1。您认为这个问题是由gcc版本引起的吗?不知道,但可能是。您确定您的Boost版本足够新,可以支持您的编译器吗?我使用最新版本的Boost1.52.0.我如何知道它是否足够新?感谢您的提醒。boost似乎支持gcc-4.7.0。但它没有说明gcc-4.7.1。是的,您是对的!当我添加“boost::geometry:”在距离前面。这很有效。你能告诉我如何避免这个错误吗?我不想一直在每个函数中添加这么长的字符。非常感谢!@user1894177:最好的方法是不要使用指令,或者至少,将它们限制在比整个文件小得多的范围内。我个人从不使用使用namespace std;
(因为std::
是一个很短的前缀,不值得去掉);对于更长的命名空间,可以给它一个短别名,例如namespace geo=boost::geometry
。是的,你说得对!当我添加“boost::geometry:”在距离前面。这很有效。你能告诉我如何避免这个错误吗?我不想一直在每个函数中添加这么长的字符。非常感谢!@user1894177:最好的方法是不要使用指令,或者至少,将它们限制在比整个文件小得多的范围内。我个人从不使用使用namespace std;
(因为std::
是一个很短的前缀,不值得去掉);对于较长的命名空间,可以给它一个短别名,例如namespace geo=boost::geometry
。