简单代码编译通不过(求两点间距离),请指教
还有很多没有标出来,请问是怎么回事呀~
#include <iostream.h>
#include <math.h>
class point
{
public:
point(float xx, float yy);
point(){cout << "拷贝函数被调用" << endl;}
Getx();
Gety();
private:
float x,y;
};
class dists
{
public:
dists(point &xx, point &yy);
Getdist();
public:
point x,y;
float dist;
}
point::point(float xx,float yy)//constructors not allowed a return type
{
x = xx;
y = yy;
}
dists::dists(point &xx, point &yy):x(xx),y(yy)
{
dist = sqrt((x.Getx() - y.Getx()) * (x.Getx() - y.Getx()) + (x.Gety()-y.Gety())*(x.Gety()-y.Gety()));
}//'-' : illegal, left operand has type 'int (__thiscall point::*)(void)'
float point::Getx()
{ //'Getx' : redefinition; different basic types
return x;
}
float point::Gety()
{//'float __thiscall point::Gety(void)' :
//overloaded function differs only by return type from 'int __thiscall point::Gety(void)'
//'Gety' : redefinition; different basic types
return y;
}
float dists::Getdist()
{
return dist;
}
main()
{
point a(1,2),b(5.2);
dists lines(a,b);
cout << lines.Getdist() << endl;//'Getdist' : error in function definition or declaration; function not called
}
问题点数:20、回复次数:14Top
1 楼wythust(begin with the end in mind)回复于 2004-04-02 22:05:22 得分 0
cout << lines.Getdist() << endl;//'Getdist' : error in function definition or declaration; function not called
如果我没有眼花的话,你的lines类在哪啊,根本没有定义嘛,那怎么调用啊??!Top
2 楼angelo23(angelo)回复于 2004-04-02 22:05:48 得分 0
#include <iostream>
#include <cmath>
using namespace std;
class point
{
public:
point(float xx, float yy);
point(){cout << "拷贝函数被调用" << endl;}
float Getx(); //返回类型
float Gety(); //同上
private:
float x,y;
};
class dists
{
public:
dists(point &xx, point &yy);
float Getdist();//返回类型
public:
point x,y;
float dist;
}; //这里少了一个;
point::point(float xx,float yy)
{
x = xx;
y = yy;
}
dists::dists(point &xx, point &yy):x(xx),y(yy)
{
dist = sqrt((x.Getx() - y.Getx()) * (x.Getx() - y.Getx()) + (x.Gety()-y.Gety())*(x.Gety()-y.Gety()));
}
float point::Getx()
{
return x;
}
float point::Gety()
{
return y;
}
float dists::Getdist()
{
return dist;
}
int main() //main()必须返回int,不可以省略
{
point a(1,2),b(5,2); //,打成.了
dists lines(a,b);
cout << lines.Getdist() << endl;
}Top
3 楼angelo23(angelo)回复于 2004-04-02 22:06:57 得分 0
wythust(小涛)好像确实……眼花了:-)
dists lines(a,b);Top
4 楼Ngod(天泽)回复于 2004-04-02 22:08:41 得分 5
#include <iostream.h>
#include <math.h>
class point
{
public:
point(float xx, float yy);
point();
float Getx();
float Gety();
private:
float x,y;
};
class dists
{
public:
dists(point &xx, point &yy);
float Getdist();
public:
point x,y;
float dist;
};
point::point(float xx,float yy)//constructors not allowed a return type
{
x = xx;
y = yy;
}
dists::dists(point &xx, point &yy):x(xx),y(yy)
{
dist = sqrt((x.Getx() - y.Getx()) * (x.Getx() - y.Getx()) + (x.Gety()-y.Gety())*(x.Gety()-y.Gety()));
}
//'-' : illegal, left operand has type 'int (__thiscall point::*)(void)'
float point::Getx()
{ //'Getx' : redefinition; different basic types
return x;
}
float point::Gety()
{//'float __thiscall point::Gety(void)' :
//overloaded function differs only by return type from 'int __thiscall point::Gety(void)'
//'Gety' : redefinition; different basic types
return y;
}
float dists::Getdist()
{
return dist;
}
main()
{
point a(1,2);
point b(5,2);
dists lines(a,b);
cout << lines.Getdist() << endl;//'Getdist' : error in function definition or declaration; function not called
}
Top
5 楼hwbin2008(影子)回复于 2004-04-02 22:31:49 得分 0
太好了,是不是初学编程都这样呀?真是一塌湖涂.
上面还有一个警告?就是:
dists::dists(point &xx, point &yy):x(xx),y(yy)
{
dist = sqrt((x.Getx() - y.Getx()) * (x.Getx() - y.Getx()) + (x.Gety()-y.Gety())*(x.Gety()-y.Gety()));
}
提示说这里要将"double"强制转换成"float",这是怎么回事呀?我没有把它们设为double型啊Top
6 楼angelo23(angelo)回复于 2004-04-02 22:40:41 得分 15
因为sqrt的函数原型是
double sqrt(double);
你传给sqrt的表达式是float的,不会引起精度损失,但是把double型的返回值赋给float型的dist,会损失精度,所以有个warningTop
7 楼jp1984(mathfrog)回复于 2004-04-03 14:53:15 得分 0
还有个warning 就是函数要返回值 ,最后少了一个return 0; 其他的angelo完全正确了,函数的返回类型为函数定义的一部分。 //love myjTop
8 楼hwbin2008(影子)回复于 2004-04-03 23:12:49 得分 0
哦,明白了,谢谢,我初学C++,请问哪里有关于这些错误信息的解释啊?Top
9 楼angelo23(angelo)回复于 2004-04-03 23:23:13 得分 0
编译器呗~或者google之~Top
10 楼angelo23(angelo)回复于 2004-04-03 23:24:18 得分 0
to jp1984
标准规定main函数必须返回int,如果不指定return 0;会自动加上,这和其它函数不一样Top
11 楼jp1984(mathfrog)回复于 2004-04-04 00:02:47 得分 0
晕。。是不是新标准。 。 我用TC++好象不会自动加上 。。//*love myj*Top
12 楼angelo23(angelo)回复于 2004-04-04 09:14:32 得分 0
我表述的不太正确,不是自动加上return 0; 是如果没有显示的返回一个值,缺省的返回0Top
13 楼newegg2002(同志们,同胞们,大学的四年,是扎实基础的四年!!)回复于 2004-04-04 09:38:26 得分 0
错误大家都改正确了,,也没什么可改的,编程最好还是细心一点,,
至于一个警告问题作一下强制类型转换,要不将dist设成double型 ,,
觉得后者还是好一点,,,Top
14 楼hwbin2008(影子)回复于 2004-04-04 22:43:21 得分 0
请问为什么void类型的指针就可以存储任意类型的地址呀?这什么是它Top




