鱼C论坛

 找回密码
 立即注册
查看: 990|回复: 4

[已解决]成员函数返回值错误

[复制链接]
发表于 2020-12-19 21:58:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这里a.GetPeri()为什么返回0

  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4. class Shape{
  5. public:
  6.         virtual float GetArea(){};
  7.         virtual float GetPeri(){};
  8. };

  9. class Rectangle:public Shape{
  10. public:
  11.         Rectangle(float w,float l): width(w),length(l){}
  12.         float GetArea(){
  13.                 return width*length;
  14.         }
  15.         float GerPeri(){
  16.                 return 2*(width+length);
  17.         }
  18.         void p(){
  19.                 cout<<width<<endl<<length<<endl;
  20.         }
  21. private:
  22.         float width;
  23.         float length;
  24. };
  25. void printinfo(Shape& a){
  26.         cout<<"面积是:"<<a.GetArea()<<endl;
  27.         cout<<"周长是:"<<a.GetPeri()<<endl;                           //这里返回的是0  
  28. }
  29. int main(){

  30.         Rectangle r(10.56,20.7);
  31.         r.p();
  32.         printinfo(r);

  33. }
复制代码
最佳答案
2020-12-21 08:41:15

函数名拼写错误
  1. class Rectangle:public Shape{
  2. public:
  3.         Rectangle(float w,float l): width(w),length(l){}
  4.         float GetArea(){
  5.                 return width*length;
  6.         }
  7.        //难道不应该是   GetPeri()  吗?
  8.         float GerPeri(){
  9.                 return 2*(width+length);
  10.         }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-19 23:49:48 From FishC Mobile | 显示全部楼层
把2*改成2. 0f试试。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-20 11:16:56 | 显示全部楼层
xieglt 发表于 2020-12-19 23:49
把2*改成2. 0f试试。

不行 还是0
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-21 08:41:15 | 显示全部楼层    本楼为最佳答案   

函数名拼写错误
  1. class Rectangle:public Shape{
  2. public:
  3.         Rectangle(float w,float l): width(w),length(l){}
  4.         float GetArea(){
  5.                 return width*length;
  6.         }
  7.        //难道不应该是   GetPeri()  吗?
  8.         float GerPeri(){
  9.                 return 2*(width+length);
  10.         }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-21 17:01:59 | 显示全部楼层
xieglt 发表于 2020-12-21 08:41
函数名拼写错误

呜呜呜呜呜原来如此 这种竟然不会报错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-7 12:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表