== 运算符重载结果不是想要的

sunrisefe 2008-06-17 04:02:09
写了两个类都用于了==重载,但是调用时不相等的对象也判断为相等了,不知道怎么回事
class Point
{
public:
float x;
float y;
public:
Point(void);
public:
~Point(void);

Point(float x,float y);
// 两点是否相等
bool operator==(const Point p)const;
};
//两点是否相等
bool Point::operator==(const Point p)const
{
if(this->x==p.x && this->y==p.y)
return true;
else
return false;
}



class Edge
{

public:
Edge(void);
Edge(Point p1,Point p2);
bool operator==(const Edge e)const;
public:
~Edge(void);
public:
// 标识该边是否为边界
bool flag;
Point p1;
Point p2;
};
//两条边相等
bool Edge::operator==(const Edge e)const
{
if((this->p1==e.p1 && this->p2==e.p2)||(this->p1==e.p2 && this->p2==e.p1))
return true;
else
return false;
}


调用Edge对象的==时,不相等边如Edge( Point(1,15),Point(0,16))==( Point(1,15),Point(3,16))返回结果为真,调用堆栈中的内容却发现:this对象的p1=(1,15),p2=(3,16)而不是(1,15),(0,16)。这是怎么回事
...全文
155 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
fallening 2008-06-19
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 sunrisefe 的回复:]
如果判断两个浮点数不相等,应该怎么做呢
[/Quote]
其实都是差不多的,不过用差的绝对值判断比较稳妥一点,
比如在判断一个float类型的数是不是0的时候
fallening 2008-06-19
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 sunrisefe 的回复:]
不是的,我发消息时,没有看到你提供的方案。对不起
[/Quote]
没关系,刚才测试了一下子,给你看看完整实现就是了
$ cat point.h
#ifndef _POINT_H
#define _POINT_H

#include <cmath>

class Point
{
private:
float x;
float y;
public:
explicit Point( const float& _x = 0, const float& _y = 0 )
{
x = _x;
y = _y;
}
~Point(){}

Point& operator = ( const Point& other )
{
x = other.x;
y = other.y;

return *this;
}

friend bool operator == ( const Point& lhs, const Point& rhs )
{
return ( fabs( lhs.x - rhs.x ) < 1E-10) && ( fabs( lhs.y - rhs.y ) < 1E-10 );
}
};


class Edge
{
private:
bool flag;
Point p1;
Point p2;
public:
explicit Edge( const Point& _p1, const Point& _p2 )
{
flag = true;
p1 = _p1;
p2 = _p2;
}
~Edge(){}

friend bool operator == ( const Edge& lhs, const Edge& rhs )
{
return ( lhs.flag == rhs.flag ) &&
( lhs.p1 == rhs.p1 ) &&
( lhs.p2 == rhs.p2 );
}

};


#endif

$ cat test.cc
#include "point.h"

#include <iostream>

using namespace std;

int main()
{
if (
Edge( Point(1,15), Point(0,16) ) ==

Edge( Point(1,15), Point(3,16))

)
cout << "true";
else
cout << "false";

return 0;
}

$ g++ -o test test.cc -Wall $CFLAGS
$ ./test
false
sunrisefe 2008-06-19
  • 打赏
  • 举报
回复
如果判断两个浮点数不相等,应该怎么做呢
sunrisefe 2008-06-19
  • 打赏
  • 举报
回复
不是的,我发消息时,没有看到你提供的方案。对不起
sunrisefe 2008-06-19
  • 打赏
  • 举报
回复
fallening ,, 你在吗,估计是你提到的问题,请问Edge类的==是否也随之修改成一样的、
fallening 2008-06-19
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 sunrisefe 的回复:]
上面提供的,都不正确。
[/Quote]
你的测试环境?
sunrisefe 2008-06-19
  • 打赏
  • 举报
回复
上面提供的,都不正确。
fallening 2008-06-19
  • 打赏
  • 举报
回复

抱歉,我写的有问题。

==是一个二元操作符,要把它声明为friend,而不是member

可能就是这里出现了问题



应该这样

class Point
{
public:
float x;
float y;
public:
Point (void);
~Point ();
Point (float x, float y);

friend bool operator == (const Point & lhs, const Point& rhs )
{
//return ( ( lhs.x == rhs.x ) && ( lhs.y == rhs.y ) );
//推荐这种写法
return( ( fabs( lhs.x - rhs.x ) < 1E-10 ) && ( fabs( lhs.x - rhs.x ) < 1E-10 ) );
}

};


另外我要提醒的是,这里出现了float类型的数据的比较



fallening 2008-06-19
  • 打赏
  • 举报
回复
$ cat point.h
class Point
{
public:
float x;
float y;
public:
Point (void);
public:
//~Point(void);
~Point ();

Point (float x, float y);
// 两点是否相等
//bool operator==(const Point p)const;
bool operator== (const Point & p) const;
};
//两点是否相等
/*
bool Point::operator==(const Point p)const
{
if(this->x==p.x && this->y==p.y)
return true;
else
return false;
}
*/
bool
Point::operator == (const Point & p) const
{
return ( x == p.x && y == p.y);
}

class Edge
{

public:
Edge (void);
Edge (Point p1, Point p2);
//bool operator==(const Edge e)const;
bool operator== (const Edge & e) const;
public:
//~Edge(void);
~Edge ();
public:
// 标识该边是否为边界
bool flag;
Point p1;
Point p2;
};

//两条边相等
bool
Edge::operator == ( const Edge& e) const
{
return (
( flag == e.flag ) &&
( ( ( p1 == e.p1 ) && ( p2 == e.p2 ) ) ||
( ( p2 == e.p1 ) && ( p1 == e.p2 ) )
)
);

//return ((this->p1 == e.p1 && this->p2 == e.p2) ? ?¦(this->p1 == e.p2
// && this->p2 == e.p1))
//return true;
//else
//return false;
}

xkyx_cn 2008-06-19
  • 打赏
  • 举报
回复

// 请使用初始化列表,不要这样赋值
Edge::Edge(Point p1,Point p2)
{
this->p1=p1;
this->p2=p2;
this->flag=false;
}
sunrisefe 2008-06-19
  • 打赏
  • 举报
回复
哪里写错了?请指教。构造函数有错吗?
andy_cai 2008-06-19
  • 打赏
  • 举报
回复
你的构造函数怎么写的?
我随手写了个,起码你举的那个例子也是正确的
sunrisefe 2008-06-19
  • 打赏
  • 举报
回复
请问我写的错在什么地方,在很多时候,结果也是正确的,但有时却不正确,我不知道错在什么地方。
kuku0122 2008-06-19
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>
#include<fstream>
#include <vector>
using namespace std;
class Point
{
private:
int x;
int y;
public:
friend bool operator==( const Point& p1, const Point& p2)
{
return ((p1.x==p2.x )&& (p1.y==p2.y) );
}
Point();
Point(float x,float y);
~Point();
};


class Edge
{

public:
Edge();
Edge(Point p1,Point p2);
friend bool operator==( const Edge& e1, const Edge& e2)
{
return((e1.p1==e2.p1 && e1.p2==e2.p2)||(e1.p1==e2.p2 && e1.p2==e2.p1)) ;
}
~Edge();
private:
// 标识该边是否为边界
bool flag;
Point p1;
Point p2;
};
Point::~Point()
{
}
Point::Point(float x,float y) : x(x), y(y)
{
}
Edge::~Edge()
{
}
Edge::Edge(Point p1,Point p2) : flag(false), p1(p1), p2(p2)
{
}

int main()
{


cout << (Edge(Point(1, 2), Point(3, 4)) == Edge(Point(1, 2), Point(3, 5))) << endl;

return 0;
}
sunrisefe 2008-06-19
  • 打赏
  • 举报
回复
xkyx_cn , 我用的如下怎么不对了?以下是我写的点和边的类,你看看==重载错在哪里。我在别的类里调用比较时,在DEBUG里明明看到实参e为((12,3),(14,0)),当前边(*iterEdge)值是((12,3),(14,4)),用
if(e == (*iterEdge))时,却通过了判断。
*********point.h**************
#pragma once
#include <math.h>
class Point
{
public:
float x;
float y;
bool flag;
public:
Point(void);
public:
~Point(void);

Point(float x,float y);

public:
// 两点是否相等
bool operator==( Point p);
};

****************point.cpp****************
#include "StdAfx.h"
#include "Point.h"

Point::Point(void)
{

}

Point::~Point(void)
{
}

Point:: Point(float x,float y)
{
this->x=x;this->y=y;this->flag=false;
}




// 两点是否相等
bool Point::operator==( Point p)
{
/*if(this->x==p.x && this->y==p.y)*/
if(x==p.x && y==p.y)
return true;
else
return false;
}

*********************Edge.h****************
#pragma once
#include "Point.h"
class Edge
{

public:
Edge(void);
Edge(Point p1,Point p2);
bool operator==( Edge e);
public:
~Edge(void);
public:
// 标识该边是否为边界
bool flag;
Point p1;
Point p2;
};
*********************Edge.cpp*************
#include "StdAfx.h"
#include "Edge.h"
#include "Point.h"


Edge::Edge(void)
: flag(false)
, p1(Point())
, p2(Point())
{
}

Edge::~Edge(void)
{
}

Edge::Edge(Point p1,Point p2)
{
this->p1=p1;
this->p2=p2;
this->flag=false;

}
bool Edge::operator==( Edge e)
{
/*if( (this->p1==e.p1 && this->p2==e.p2) || (this->p1==e.p2 && this->p2==e.p1) )*/
if((p1==e.p1 && p2==e.p2)|| (p1==e.p2 && p2==e.p1))
return true;
else
return false;
}
XiaoG602 2008-06-17
  • 打赏
  • 举报
回复
先无语一下………………
稍微改了一下,看看吧

class Point
{
public:
float x;
float y;
Point(Point &p){x=p.x;y=p.y;}
Point(float x1,float y1){x=x1;y=y1;}
// 两点是否相等
bool operator==(Point p);
};

//两点是否相等
bool Point::operator==(Point p)
{
if(x==p.x && y==p.y)
return true;
else
return false;
}


class Edge
{
public:
Edge(Point p11,Point p22):p1(p11),p2(p22){}
bool operator==(Edge e);
// 标识该边是否为边界
bool flag;
Point p1;
Point p2;
};
//两条边相等
bool Edge::operator==(Edge e)
{
if((p1==e.p1 &&p2==e.p2)||(p1==e.p2 && p2==e.p1))
return true;
else
return false;
}
xkyx_cn 2008-06-17
  • 打赏
  • 举报
回复
如此可以得到正确结果

但原因不在于const &, 从一开始就是lz构造的比较语句不对
Edge( Point(1,15),Point(0,16))==( Point(1,15),Point(3,16))
仔细检查一下


#include <iostream>

using namespace std;

class Point
{
public:
float x;
float y;
public:
Point(void) {}
public:
~Point(void) {}

Point(float x,float y) : x(x), y(y) {}
// 两点是否相等
bool operator==(const Point& p)const;
};
//两点是否相等
bool Point::operator==(const Point& p)const
{
if(this->x==p.x && this->y==p.y)
return true;
else
return false;
}

class Edge
{

public:
Edge(void) {}
Edge(Point p1,Point p2) : p1(p1), p2(p2) {}
bool operator==(const Edge& e)const;
public:
~Edge(void) {}
public:
// 标识该边是否为边界
bool flag;
Point p1;
Point p2;
};
//两条边相等
bool Edge::operator==(const Edge& e)const
{
if((this->p1==e.p1 && this->p2==e.p2) || (this->p1==e.p2 && this->p2==e.p1))
return true;
else
return false;
}

int main(void)
{
cout << (Edge(Point(1, 2), Point(3, 4)) == Edge(Point(1, 2), Point(3, 5))) << endl;

return 0;
}
xkyx_cn 2008-06-17
  • 打赏
  • 举报
回复
operator==的形参请使用const &类型,如:
const Point& e
const Edge& e
artman 2008-06-17
  • 打赏
  • 举报
回复
操作符重载,最好使用友元函数, 而非成员函数.
taodm 2008-06-17
  • 打赏
  • 举报
回复
自己用调试器走一遍就知道了。
使用调试器,是每个程序员必须掌握的基本技能。
加载更多回复(2)

64,648

社区成员

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

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