C++中为何无法通过double返回值调用成员函数?(继承类场景)
C++继承问题:派生类Cylinder无法正确计算并显示面积和体积
正在练习C++继承,将类拆分到单独文件,基类为Circle,派生类为Cylinder。Circle对象功能正常,但Cylinder无法正确计算并显示面积、体积,当前遇到编译错误。
现有代码
circle.cpp
#include "circle.h" #include <cmath> using namespace std; Circle Circle::area() { return M_PI * pow(r, 2); } void Circle::getArea() { cout << "Area = " << r << " m^2"; }
cylinder.cpp
#include "cylinder.h" #include <cmath> using namespace std; double Cylinder::area() { return 2 * M_PI * r * h; } void Cylinder::getArea() { cout << "Area = " << h; }
main.cpp
#include <iostream> #include "circle.h" #include "cylinder.h" using namespace std; int main() { Circle A; A.view(); Circle A1(8); A1.view(); Cylinder B; B.view(); Cylinder B1(4, 6); B1.view(); //A.area().getArea(); //cout << endl; //A.circumference().getCircum(); //cout << endl; //A1.area().getArea(); //cout << endl; //A1.circumference().getCircum(); B.area().getArea(); return 0; }
编译错误
main.cpp: In function ‘int main()’: main.cpp:26:14: error: request for member ‘getArea’ in ‘B.Cylinder::area()’, which is of non-class type ‘double’ 26 | B.area().getArea(); | ^~~~~~~
修复建议
1. 修正基类Circle的方法设计
Circle的area()返回Circle对象完全不符合逻辑,面积应该是数值类型。同时getArea()当前输出的是半径r,不是面积,需要修正:
补充并修正circle.h
#ifndef CIRCLE_H #define CIRCLE_H #include <iostream> using namespace std; class Circle { protected: double r; // 设为protected,让派生类可直接访问 public: Circle() : r(0) {} Circle(double radius) : r(radius) {} void view() { cout << "Circle radius: " << r << endl; } // 返回double类型的面积值 double area() { return M_PI * r * r; } // 直接计算并输出面积 void getArea() { cout << "Circle Area = " << area() << " m^2" << endl; } }; #endif
修正circle.cpp
#include "circle.h" #include <cmath> using namespace std; double Circle::area() { return M_PI * r * r; } void Circle::getArea() { cout << "Circle Area = " << area() << " m^2" << endl; }
2. 修正派生类Cylinder的方法
Cylinder的area()当前只计算了侧面积,实际应该计算表面积(侧面积+两个底面积),同时补充体积计算方法,并修正输出逻辑:
补充并修正cylinder.h
#ifndef CYLINDER_H #define CYLINDER_H #include "circle.h" class Cylinder : public Circle { private: double h; // 圆柱高度 public: Cylinder() : Circle(), h(0) {} Cylinder(double radius, double height) : Circle(radius), h(height) {} void view() { cout << "Cylinder radius: " << r << ", height: " << h << endl; } // 计算表面积:侧面积 + 2个底面积 double area() { return 2 * M_PI * r * h + 2 * Circle::area(); } // 计算体积 double volume() { return Circle::area() * h; } // 输出表面积 void getArea() { cout << "Cylinder Surface Area = " << area() << " m^2" << endl; } // 输出体积 void getVolume() { cout << "Cylinder Volume = " << volume() << " m^3" << endl; } }; #endif
修正cylinder.cpp
#include "cylinder.h" #include <cmath> using namespace std; double Cylinder::area() { return 2 * M_PI * r * h + 2 * Circle::area(); } double Cylinder::volume() { return Circle::area() * h; } void Cylinder::getArea() { cout << "Cylinder Surface Area = " << area() << " m^2" << endl; } void Cylinder::getVolume() { cout << "Cylinder Volume = " << volume() << " m^3" << endl; }
3. 修正main函数的调用逻辑
不能用B.area().getArea()这种链式调用(area()返回的是double,不是类对象),直接调用getArea()、getVolume()即可:
修正main.cpp
#include <iostream> #include "circle.h" #include "cylinder.h" using namespace std; int main() { Circle A; A.view(); Circle A1(8); A1.view(); Cylinder B; B.view(); Cylinder B1(4, 6); B1.view(); // 调用Circle的面积输出方法 A.getArea(); A1.getArea(); // 调用Cylinder的表面积和体积输出方法 B.getArea(); B.getVolume(); B1.getArea(); B1.getVolume(); return 0; }
关键说明
- 将Circle的半径
r设为protected,确保派生类Cylinder可以直接访问,避免重复定义。 - 修正方法返回值逻辑:面积、体积这类属性应该返回数值类型(double),而非类对象。
- 派生类Cylinder的表面积计算要包含底面积,符合几何定义。
- 调用方法时直接调用
getArea()、getVolume(),这类方法内部完成计算并输出,逻辑更清晰。
内容的提问来源于stack exchange,提问作者blulow
相关产品推荐
相关产品推荐

