初学者遭遇离奇错误——求两点间的距离(C++)


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

  1. #include <iostream>  
  2. #include <math.h>  
  3. using namespace std;  
  4. class point  
  5. {  
  6. public:  
  7.     double x;  
  8.     double y;  
  9. };  
  10. double distance(point p1,point p2);  
  11.   
  12. int main()  
  13. {  
  14.     point p1= {3,5},p2= {6,9};  
  15.     cout<<distance(p1,p2);  
  16. }  
  17.   
  18. double distance(point p1,point p2)  
  19. {  
  20.     double d=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));  
  21.     return d;  
  22. }  
#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文件中,错误一大堆:

  1. ||=== example, Debug ===|  
  2. D:\C++\codeBlock\example\example.cpp|15|instantiated from here|  
  3. 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'|  
  4. 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'|  
  5. 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'|  
  6. 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'|  
  7. 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'|  
  8. ||=== Build finished: 5 errors, 0 warnings ===|  
||=== 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::,程序如下:

  1. #include <iostream>  
  2. #include <math.h>  
  3. //using namespace std;//不再用命名空间std  
  4. class point  
  5. {  
  6. public:  
  7.     double x;  
  8.     double y;  
  9. };  
  10. double distance(point p1,point p2);  
  11.   
  12. int main()  
  13. {  
  14.     point p1= {3,5},p2= {6,9};  
  15.     std::cout<<distance(p1,p2);  //保证编译系统知道用std命名空间中的cout  
  16. }  
  17.   
  18. double distance(point p1,point p2)  
  19. {  
  20.     double d=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));  
  21.     return d;  
  22. }  
#include <iostream>
#include <math.h>
//using namespace std;//不再用命名空间std
class 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是当前程序中定义的名字。问题解决就此解决,程序如下:

  1. #include <iostream>  
  2. #include <math.h>  
  3. using namespace std;  
  4. class point  
  5. {  
  6. public:  
  7.     double x;  
  8.     double y;  
  9. };  
  10. double distance(point p1,point p2);  
  11.   
  12. int main()  
  13. {  
  14.     point p1= {3,5},p2= {6,9};  
  15.     cout<<::distance(p1,p2);//指定distance不是别处的,就是本文件中定义的  
  16. }  
  17.   
  18. double distance(point p1,point p2)  
  19. {  
  20.     double d=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));  
  21.     return d;  
  22. }  
#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. 1>------ 已启动生成: 项目: example, 配置: Debug Win32 ------  
  2. 1>正在编译...  
  3. 1>example.cpp  
  4. 1>d:\program files\microsoft visual studio 9.0\vc\include\xutility(764) : error C2039: “iterator_category”: 不是“point”的成员  
  5. 1>        d:\c++\vs2008 project\example\example\example.cpp(5) : 参见“point”的声明  
  6. 1>        d:\c++\vs2008 project\example\example\example.cpp(16): 参见对正在编译的类 模板 实例化“std::iterator_traits<_Iter>”的引用  
  7. 1>        with  
  8. 1>        [  
  9. 1>            _Iter=point  
  10. 1>        ]  
  11. (此处省略N多的提示)  
  12.   
  13.   
  14. 1>生成日志保存在“file://d:\C++\VS2008 project\example\example\Debug\BuildLog.htm”  
  15. 1>example - 25 个错误,0 个警告  
  16. ========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========  
1>------ 已启动生成: 项目: example, 配置: Debug Win32 ------
1>正在编译...
1>example.cpp
1>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>        with
1>        [
1>            _Iter=point
1>        ]
(此处省略N多的提示)


1>生成日志保存在“file://d:\C++\VS2008 project\example\example\Debug\BuildLog.htm”
1>example - 25 个错误,0 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========


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


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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值