(求解)友元函数的使用解决办法
(求解)友元函数的使用
直接上代码:
编译无法通过,求解那里除了错误。
------解决方案--------------------
不要使用using namespace std; 可能会于distance函数std::distance冲突。
或者把distance改名试试。
------解决方案--------------------
distance名字冲突,换个吧
------解决方案--------------------
+11111,把名字改了
return sqrt((left.x-right.x)*(left.x-right.x)+(left.y-left.y)*(left.y-left.y));
红色的了改了
直接上代码:
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
class Point
{
private:
float x;
float y;
public:
Point()
{
x = 0;
y = 0;
}
Point (float a, float b):x(a), y(b){}
Point &operator-(const Point& other)
{
Point tem;
tem.x = x - other.x ;
tem.y = y - other.y;
return tem;
}
void disp()
{
cout<<"Point:";
cout<<"("<<x<<","<<y<<")"<<endl;
}
friend float distance(Point &left, Point& right);
};
float distance(Point &left, Point &right)
{
return sqrt((left.x-right.x)*(left.x-right.x)+(left.y-left.y)*(left.y-left.y));
}
int main(int argc, char*argv[])
{
Point objone;
Point objtwo(1,1);
Point objthree;
objone.disp();
objtwo.disp();
cout<<distance(objone,objtwo)<<endl;
return 0;
}
编译无法通过,求解那里除了错误。
------解决方案--------------------
不要使用using namespace std; 可能会于distance函数std::distance冲突。
或者把distance改名试试。
------解决方案--------------------
distance名字冲突,换个吧
------解决方案--------------------
+11111,把名字改了
return sqrt((left.x-right.x)*(left.x-right.x)+(left.y-left.y)*(left.y-left.y));
红色的了改了