这是个bug是怎么回事?

kuillldan 2009-11-29 05:30:32
以下是我的源代码,程序很简单,但不知怎么错了。。求高人指点。

#include <iostream>
#include <cmath>
using namespace std;

class Point
{
double X,Y;//描述点的坐标

public :
Point(){ X = Y = 0.0;};
void Set()
{
cout<<"请输入点的横坐标和纵坐标:"<<endl;
cin>>X>>Y;
}
double GetX()const { return X; };
double GetY()const { return Y; };
};

double distance(const Point &P1,const Point &P2);//把distance换成dist就没错了。。。

void main()
{
Point point1,point2;
double dis;//记录两点的距离
point1.Set();
point2.Set();
dis = distance(point1,point2);//把distance换成dist就没错了。。。
cout<<"两点的距离是:"<<dis<<endl;
}

double distance(const Point &P1, const Point &P2)//把distance换成dist就没错了。。。
{
double x,y;
x = (P1.GetX() - P2.GetX());
y = (P1.GetY() - P2.GetY());
x = fabs(x*x);
y = fabs(y*y);
return sqrt(x+y);
}


以上程序不能编译通过,我把distance函数名换成dist就没错了。。。为什么啊?

显示错误如下:
1>------ 已启动生成: 项目: test, 配置: Debug Win32 ------
1>正在编译...
1>test.cpp
1>d:\安装文件\visual studio 2008\vc\include\xutility(764) : error C2039: “iterator_category”: 不是“Point”的成员
1> d:\code\test\test\test.cpp(17) : 参见“Point”的声明
1> d:\code\test\test\test.cpp(39): 参见对正在编译的类 模板 实例化“std::iterator_traits<_Iter>”的引用
1> with
1> [
1> _Iter=Point
1> ]
1>d:\安装文件\visual studio 2008\vc\include\xutility(764) : error C2146: 语法错误 : 缺少“;”(在标识符“iterator_category”的前面)
1>d:\安装文件\visual studio 2008\vc\include\xutility(764) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>d:\安装文件\visual studio 2008\vc\include\xutility(764) : error C2602: “std::iterator_traits<_Iter>::iterator_category”不是“std::iterator_traits<_Iter>”基类的成员
1> with
1> [
1> _Iter=Point
1> ]
1> d:\安装文件\visual studio 2008\vc\include\xutility(764) : 参见“std::iterator_traits<_Iter>::iterator_category”的声明
1> with
1> [
1> _Iter=Point
1> ]
1>d:\安装文件\visual studio 2008\vc\include\xutility(764) : error C2868: “std::iterator_traits<_Iter>::iterator_category”: 非法的 using 声明语法;应输入限定名
1> with
1> [
1> _Iter=Point
1> ]
1>d:\安装文件\visual studio 2008\vc\include\xutility(765) : error C2039: “value_type”: 不是“Point”的成员
1> d:\code\test\test\test.cpp(17) : 参见“Point”的声明
1>d:\安装文件\visual studio 2008\vc\include\xutility(765) : error C2146: 语法错误 : 缺少“;”(在标识符“value_type”的前面)
1>d:\安装文件\visual studio 2008\vc\include\xutility(765) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>d:\安装文件\visual studio 2008\vc\include\xutility(765) : error C2602: “std::iterator_traits<_Iter>::value_type”不是“std::iterator_traits<_Iter>”基类的成员
1> with
1> [
1> _Iter=Point
1> ]
1> d:\安装文件\visual studio 2008\vc\include\xutility(765) : 参见“std::iterator_traits<_Iter>::value_type”的声明
1> with
1> [
1> _Iter=Point
1> ]
1>d:\安装文件\visual studio 2008\vc\include\xutility(765) : error C2868: “std::iterator_traits<_Iter>::value_type”: 非法的 using 声明语法;应输入限定名
1> with
1> [
1> _Iter=Point
1> ]
1>d:\安装文件\visual studio 2008\vc\include\xutility(766) : error C2039: “difference_type”: 不是“Point”的成员
1> d:\code\test\test\test.cpp(17) : 参见“Point”的声明
1>d:\安装文件\visual studio 2008\vc\include\xutility(766) : error C2146: 语法错误 : 缺少“;”(在标识符“difference_type”的前面)
1>d:\安装文件\visual studio 2008\vc\include\xutility(766) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>d:\安装文件\visual studio 2008\vc\include\xutility(766) : error C2602: “std::iterator_traits<_Iter>::difference_type”不是“std::iterator_traits<_Iter>”基类的成员
1> with
1> [
1> _Iter=Point
1> ]
1> d:\安装文件\visual studio 2008\vc\include\xutility(766) : 参见“std::iterator_traits<_Iter>::difference_type”的声明
1> with
1> [
1> _Iter=Point
1> ]
1>d:\安装文件\visual studio 2008\vc\include\xutility(766) : error C2868: “std::iterator_traits<_Iter>::difference_type”: 非法的 using 声明语法;应输入限定名
1> with
1> [
1> _Iter=Point
1> ]
1>d:\安装文件\visual studio 2008\vc\include\xutility(768) : error C2039: “pointer”: 不是“Point”的成员
1> d:\code\test\test\test.cpp(17) : 参见“Point”的声明
1>d:\安装文件\visual studio 2008\vc\include\xutility(768) : error C2146: 语法错误 : 缺少“;”(在标识符“pointer”的前面)
1>d:\安装文件\visual studio 2008\vc\include\xutility(768) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>d:\安装文件\visual studio 2008\vc\include\xutility(768) : error C2602: “std::iterator_traits<_Iter>::pointer”不是“std::iterator_traits<_Iter>”基类的成员
1> with
1> [
1> _Iter=Point
1> ]
1> d:\安装文件\visual studio 2008\vc\include\xutility(768) : 参见“std::iterator_traits<_Iter>::pointer”的声明
1> with
1> [
1> _Iter=Point
1> ]
1>d:\安装文件\visual studio 2008\vc\include\xutility(768) : error C2868: “std::iterator_traits<_Iter>::pointer”: 非法的 using 声明语法;应输入限定名
1> with
1> [
1> _Iter=Point
1> ]
1>d:\安装文件\visual studio 2008\vc\include\xutility(769) : error C2039: “reference”: 不是“Point”的成员
1> d:\code\test\test\test.cpp(17) : 参见“Point”的声明
1>d:\安装文件\visual studio 2008\vc\include\xutility(769) : error C2146: 语法错误 : 缺少“;”(在标识符“reference”的前面)
1>d:\安装文件\visual studio 2008\vc\include\xutility(769) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>d:\安装文件\visual studio 2008\vc\include\xutility(769) : error C2602: “std::iterator_traits<_Iter>::reference”不是“std::iterator_traits<_Iter>”基类的成员
1> with
1> [
1> _Iter=Point
1> ]
1> d:\安装文件\visual studio 2008\vc\include\xutility(769) : 参见“std::iterator_traits<_Iter>::reference”的声明
1> with
1> [
1> _Iter=Point
1> ]
1>d:\安装文件\visual studio 2008\vc\include\xutility(769) : error C2868: “std::iterator_traits<_Iter>::reference”: 非法的 using 声明语法;应输入限定名
1> with
1> [
1> _Iter=Point
1> ]
1>生成日志保存在“file://d:\CODE\test\test\Debug\BuildLog.htm”
1>test - 25 个错误,0 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========


求高人指点。

...全文
187 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kuillldan 2009-11-29
  • 打赏
  • 举报
回复
谢楼上几位!
jackyjkchen 2009-11-29
  • 打赏
  • 举报
回复
在std下,可能已经有distance这个名字了,改成这样也行,保证distance是全局


#include <iostream>
#include <cmath>

class Point
{
double X,Y;//描述点的坐标

public :
Point(){ X = Y = 0.0;};
void Set()
{
std::cout<<"请输入点的横坐标和纵坐标:"<<std::endl;
std::cin>>X>>Y;
}
double GetX()const { return X; };
double GetY()const { return Y; };
};

double distance(const Point &P1,const Point &P2);//把distance换成dist就没错了。。。

void main()
{
Point point1,point2;
double dis;//记录两点的距离
point1.Set();
point2.Set();
dis = distance(point1,point2);//把distance换成dist就没错了。。。
std::cout<<"两点的距离是:"<<dis<<std::endl;
}

double distance(const Point &P1, const Point &P2)//把distance换成dist就没错了。。。
{
double x,y;
x = (P1.GetX() - P2.GetX());
y = (P1.GetY() - P2.GetY());
x = fabs(x*x);
y = fabs(y*y);
return sqrt(x+y);
}


sduxiaoxiang 2009-11-29
  • 打赏
  • 举报
回复
名字冲突
std中有这个
cometeor 2009-11-29
  • 打赏
  • 举报
回复
跟std::distance冲突了,把using namespace std去掉就行
airtrack 2009-11-29
  • 打赏
  • 举报
回复
名字冲突,std名字空间里面有distance。

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧