原出处:http://blog.csdn.net/sxhelijian/article/details/8859653?utm_source=tuicool&utm_medium=referral
有学生向我求助,他编了一个程序,设计一个“点”类,求出两点距离的程序。程序看着没有问题,却出了一大堆的错误。程序如下:
- #include
- #include
- 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<
- }
-
- double distance(point p1,point p2)
- {
- double d&#61;sqrt((p1.x-p2.x)*(p1.x-p2.x)&#43;(p1.y-p2.y)*(p1.y-p2.y));
- return d;
- }
在codeBlocks下编译&#xff0c;错误直接引到stl_iterator_base_types.h文件中&#xff0c;错误一大堆&#xff1a;
- ||&#61;&#61;&#61; example, Debug &#61;&#61;&#61;|
- D:\C&#43;&#43;\codeBlock\example\example.cpp|15|instantiated from here|
- d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c&#43;&#43;\bits\stl_iterator_base_types.h|127|error: no type named &#39;iterator_category&#39; in &#39;class point&#39;|
- d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c&#43;&#43;\bits\stl_iterator_base_types.h|128|error: no type named &#39;value_type&#39; in &#39;class point&#39;|
- d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c&#43;&#43;\bits\stl_iterator_base_types.h|129|error: no type named &#39;difference_type&#39; in &#39;class point&#39;|
- d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c&#43;&#43;\bits\stl_iterator_base_types.h|130|error: no type named &#39;pointer&#39; in &#39;class point&#39;|
- d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c&#43;&#43;\bits\stl_iterator_base_types.h|131|error: no type named &#39;reference&#39; in &#39;class point&#39;|
- ||&#61;&#61;&#61; Build finished: 5 errors, 0 warnings &#61;&#61;&#61;|
对于这样的问题&#xff0c;初学C&#43;&#43;的同学肯定直接蒙。这里的问题出在命名空间中名字的冲突&#xff0c;再多说一些&#xff0c;与STL也有些关系。不过&#xff0c;解决这样的问题并不一定得知道这么多。我还是试着与大家绕开这个环节&#xff0c;从其他途径找点感觉。
光标置到“D:\C&#43;&#43;\codeBlock\example\example.cpp|15|instantiated from here|”一行&#xff0c;双击&#xff0c;发现错误在程序的第15行。鼠标放到15行的distance函数上时&#xff0c;浮现出了一行提示&#xff0c;见图&#xff1a;
看出了一点疑惑&#xff1a;distance不是在这个程序中编的自定义函数吗&#xff1f;怎么识别成了std::distance(...,...)&#xff1f;
这就是问题的根源&#xff01;编译器没有将distance当作自定义函数处理&#xff01;至于进一步的解释不再深入&#xff0c;抓住这个要点&#xff0c;程序就可以改好了。
修改方法之一&#xff1a;既然函数名字上出问题&#xff0c;试试改个名字&#xff1f;将distance改个名字&#xff0c;如dist&#xff0c;一切正常。
修改方法之二&#xff1a;凭什么让我改&#xff1f;distance多好的一个函数名&#xff08;不过提醒&#xff0c;可以自学一下命名空间了&#xff0c;此是好机会&#xff0c;不必等着老师讲。&#xff09;需要做的工作是&#xff0c;不用std命名空间&#xff08;删除或注释掉using namespace std;一行&#xff09;然后在依赖std的cout前加上std::&#xff0c;程序如下&#xff1a;
- #include
- #include
-
- class point
- {
- public:
- double x;
- double y;
- };
- double distance(point p1,point p2);
-
- int main()
- {
- point p1&#61; {3,5},p2&#61; {6,9};
- std::cout<
- }
-
- double distance(point p1,point p2)
- {
- double d&#61;sqrt((p1.x-p2.x)*(p1.x-p2.x)&#43;(p1.y-p2.y)*(p1.y-p2.y));
- return d;
- }
修改方法之三&#xff1a;方法二有点自私了。std中有不少常用的东东&#xff0c;就此全都得写std::&#xff0c;这个程序中无妨&#xff0c;如果再大些的程序呢&#xff1f;胳膊不必和大腿拧&#xff0c;换种思路&#xff0c;也是一样。将distance在调用时&#xff0c;写作为::distance&#xff0c;指出distance是当前程序中定义的名字。问题解决就此解决&#xff0c;程序如下&#xff1a;
- #include
- #include
- using namespace std;
- class point
- {
- public:
- double x;
- double y;
- };
- double distance(point p1,point p2);
-
- int main()
- {
- point p1&#61; {3,5},p2&#61; {6,9};
- cout<<::distance(p1,p2);
- }
-
- double distance(point p1,point p2)
- {
- double d&#61;sqrt((p1.x-p2.x)*(p1.x-p2.x)&#43;(p1.y-p2.y)*(p1.y-p2.y));
- return d;
- }
修改方法之四&#xff1a;前三种方法中&#xff0c;个人倾向于第一种&#xff0c;山不转水转&#xff0c;换个名字也妨。其实这也不是最好的。原始的程序中定义了类&#xff0c;但只有数据成员&#xff0c;没有成员函数&#xff0c;像求距离之类的&#xff0c;设计为成员函数多好。面向对象的机制就是为了信息封装等特性的&#xff0c;为何要如此浪费&#xff1f;这个程序我就不写了&#xff0c;请自行解决。
补充&#xff1a;用其他编程环境时&#xff0c;观察和修改的方法也类似&#xff0c;例如在VS2008下编译&#xff0c;错误居然有25个之多&#xff1a;
- 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&#43;&#43;\vs2008 project\example\example\example.cpp(5) : 参见“point”的声明
- 1> d:\c&#43;&#43;\vs2008 project\example\example\example.cpp(16): 参见对正在编译的类 模板 实例化“std::iterator_traits<_Iter>”的引用
- 1> with
- 1> [
- 1> _Iter&#61;point
- 1> ]
- &#xff08;此处省略N多的提示&#xff09;
-
-
- 1>生成日志保存在“file://d:\C&#43;&#43;\VS2008 project\example\example\Debug\BuildLog.htm”
- 1>example - 25 个错误&#xff0c;0 个警告
- &#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61; 生成: 成功 0 个&#xff0c;失败 1 个&#xff0c;最新 0 个&#xff0c;跳过 0 个 &#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;
在源程序中&#xff0c;鼠标光临distance函数时&#xff0c;可以看出编译器对distance函数有两种解释&#xff0c;如下图&#xff1a;
编译器对此局面真的很迷茫了。余下的修改思路相同&#xff0c;不再罗嗦。