主题:[讨论]以下程序为何输出结果不变?
至善OK
[专家分:0] 发布于 2012-06-18 19:15:00
#include <iostream>
using namespace std;
class Point
{
private:
double x;
double y;
public:
Point(double a=0.0,double b=0.0):x(a),y(b){}
friend double distance(Point &left,Point &right);
};
double distance(Point &left,Point &right)
{
double d=left.x+right.x+left.y+right.y;
return d;
}
int main()
{
Point A1(3,4);
Point A2(5,6);
cout<<distance(&A1,&A2)<<endl;
system("pause");
return 0;
}
谢谢各位了!
回复列表 (共4个回复)
沙发
至善OK [专家分:0] 发布于 2012-06-18 19:16:00
补充下:我用的VS2010
板凳
ZBC19921123 [专家分:320] 发布于 2012-06-18 23:34:00
#include <iostream>
using namespace std;
class Point
{
private:
double x;
double y;
public:
Point(double a=0.0,double b=0.0):x(a),y(b){}
friend double distance(Point &left,Point &right);
};
double distance(Point &left,Point &right)
{
double d=left.x+right.x+left.y+right.y;
return d;
}
int main()
{
Point A1(3,4);
Point A2(5,6);
cout<<distance(&A1,&A2)<<endl;
system("pause");
return 0;
}
distance(&A1,&A2)这句用到了函数模板:故你定义的diatance()函数相当于没用。
其模板如下:
[color=FF0000]template<typename _InputIterator>
inline typename iterator_traits<_InputIterator>::difference_type
distance(_InputIterator __first, _InputIterator __last)
{
// concept requirements -- taken care of in __distance
return std::__distance(__first, __last,
std::__iterator_category(__first));
}
template<typename _RandomAccessIterator>
inline typename iterator_traits<_RandomAccessIterator>::difference_type
__distance(_RandomAccessIterator __first, _RandomAccessIterator __last,
random_access_iterator_tag)
{
// concept requirements
__glibcxx_function_requires(_RandomAccessIteratorConcept<
_RandomAccessIterator>)
return __last - __first;
}[/color]
3 楼
miaoyong [专家分:760] 发布于 2012-06-19 11:13:00
出仕么问题了? 输出结果不对吗?
我感觉你构造 对象的时候有问题;
Point A1(3,4); // Point A1(3.0,4.0) ; 这样看看
Point A2(5,6); // Point A2(5.0,6.0);
4 楼
至善OK [专家分:0] 发布于 2012-06-19 16:05:00
谢谢了!
[em80]
我来回复