CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C++ 语言

简单代码编译通不过(求两点间距离),请指教

楼主hwbin2008(影子)2004-04-02 21:53:01 在 C/C++ / C++ 语言 提问

还有很多没有标出来,请问是怎么回事呀~  
  #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

相关问题

  • 为什么这末简单的代码都编译不过??
  • 一个简单的c代码编译不过的问题。
  • 很简单的一段代码也编译不通过,郁闷?
  • 新手,写一个简单的代码,但编译出错
  • 郁闷,这样简单的简短代码,编译通不过。
  • 大家来看看这个简单的代码,怎么编译出来老出问题啊!!!
  • 求教!如何用.NET编译最简单的cpp代码?然后生成exe文件?
  • 提一个简单的问题:为什么这段代码在cpp时编译不通过?
  • 20行的简单pro*c代码为啥编译通不过,帮俺找出原因就给分.
  • 见鬼,这么简单的代码也会编译出错~~~大家帮我看看错在哪里了??

关键词

  • gety
  • point
  • getx
  • dists
  • float
  • yy
  • xx
  • thiscall
  • different
  • cout

得分解答快速导航

  • 帖主:hwbin2008
  • Ngod
  • angelo23

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo