求两点间的距离(C++)

  有学生向我求助,他编了一个程序,设计一个“点”类,求出两点距离的程序。程序看着没有问题,却出了一大堆的错误。程序如下:

#include <iostream>#include <math.h>using namespace std;class point{public:double x;double y;};double distance(point p1,point p2);int main(){point p1= {3,5},p2= {6,9};cout<<distance(p1,p2);}double distance(point p1,point p2){double d=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));return d;}

  在codeBlocks下编译,错误直接引到stl_iterator_base_types.h文件中,错误一大堆:

||=== example, Debug ===|D:\C++\codeBlock\example\example.cpp|15|instantiated from here|d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_iterator_base_types.h|127|error: no type named ‘iterator_category’ in ‘class point’|d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_iterator_base_types.h|128|error: no type named ‘value_type’ in ‘class point’|d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_iterator_base_types.h|129|error: no type named ‘difference_type’ in ‘class point’|d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_iterator_base_types.h|130|error: no type named ‘pointer’ in ‘class point’|d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_iterator_base_types.h|131|error: no type named ‘reference’ in ‘class point’|||=== Build finished: 5 errors, 0 warnings ===|  对于这样的问题,初学C++的同学肯定直接蒙。这里的问题出在命名空间中名字的冲突,再多说一些,与STL也有些关系。不过,解决这样的问题并不一定得知道这么多。我还是试着与大家绕开这个环节,从其他途径找点感觉。

  光标置到“D:\C++\codeBlock\example\example.cpp|15|instantiated from here|”一行,双击,发现错误在程序的第15行。鼠标放到15行的distance函数上时,浮现出了一行提示,见图:

  看出了一点疑惑:distance不是在这个程序中编的自定义函数吗?怎么识别成了std::distance(…,…)?

  这就是问题的根源!编译器没有将distance当作自定义函数处理!至于进一步的解释不再深入,抓住这个要点,程序就可以改好了。

  修改方法之一:既然函数名字上出问题,试试改个名字?将distance改个名字,如dist,一切正常。

  修改方法之二:凭什么让我改?distance多好的一个函数名(不过提醒,可以自学一下命名空间了,此是好机会,不必等着老师讲。)需要做的工作是,不用std命名空间(删除或注释掉using namespace std;一行)然后在依赖std的cout前加上std::,程序如下:

#include <iostream>#include <math.h>//using namespace std;//不再用命名空间stdclass point{public:double x;double y;};double distance(point p1,point p2);int main(){point p1= {3,5},p2= {6,9};std::cout<<distance(p1,p2); //保证编译系统知道用std命名空间中的cout}double distance(point p1,point p2){double d=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));return d;}  修改方法之三:方法二有点自私了。std中有不少常用的东东,就此全都得写std::,这个程序中无妨,如果再大些的程序呢?胳膊不必和大腿拧,换种思路,也是一样。将distance在调用时,写作为::distance,指出distance是当前程序中定义的名字。问题解决就此解决,程序如下:#include <iostream>#include <math.h>using namespace std;class point{public:double x;double y;};double distance(point p1,point p2);int main(){point p1= {3,5},p2= {6,9};cout<<::distance(p1,p2);//指定distance不是别处的,就是本文件中定义的}double distance(point p1,point p2){double d=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));return d;}  修改方法之四:前三种方法中,个人倾向于第一种,山不转水转,换个名字也妨。其实这也不是最好的。原始的程序中定义了类,但只有数据成员,没有成员函数,像求距离之类的,设计为成员函数多好。面向对象的机制就是为了信息封装等特性的,为何要如此浪费?这个程序我就不写了,请自行解决。

  补充:用其他编程环境时,,观察和修改的方法也类似,例如在VS2008下编译,错误居然有25个之多:

1>—— 已启动生成: 项目: example, 配置: Debug Win32 ——1>正在编译…1>example.cpp1>d:\program files\microsoft visual studio 9.0\vc\include\xutility(764) : error C2039: “iterator_category”: 不是“point”的成员1>d:\c++\vs2008 project\example\example\example.cpp(5) : 参见“point”的声明1>d:\c++\vs2008 project\example\example\example.cpp(16): 参见对正在编译的类 模板 实例化“std::iterator_traits<_Iter>”的引用1>with1>[1>_Iter=point1>](此处省略N多的提示)1>生成日志保存在“file://d:\C++\VS2008 project\example\example\Debug\BuildLog.htm”1>example – 25 个错误,0 个警告========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========

  在源程序中,鼠标光临distance函数时,可以看出编译器对distance函数有两种解释,如下图:

  编译器对此局面真的很迷茫了。余下的修改思路相同,不再罗嗦。

生命太过短暂,今天放弃了明天不一定能得到

求两点间的距离(C++)

相关文章:

你感兴趣的文章:

标签云: