You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

单.cpp文件定义多类时,下层类函数无法被main访问的问题求助

Fixing Your C++ OOP Program Issues

Let's walk through the problems in your code and get it working properly:

Key Issues Identified

  • Private Member Access in Circle Class: In C++, when you define a class using the class keyword, all members (variables and functions) are private by default. That means your setR, area, and circumference functions can't be called from main()—they're only accessible within the Circle class itself.
  • Syntax Error in Main: There's a typo in the circle area output line: cout << "The area of the circle is " << c.area(); << "\n"l; has an extra semicolon after c.area() and a mistyped newline ("\n"l instead of "\n";).
  • Inefficient Pi Declaration: Your pi variable is a non-static member of Circle, which means every Circle object will store its own copy. Since pi is a constant, making it a const static member is better practice.

Corrected Code

#include <iostream>
#include <string>
using namespace std;

/*----------------RECTANGLE CLASS----------------*/
class rectangle {
    int w, h;
public:
    void setVals(int, int);
    int area();
    int perimeter();
};

void rectangle::setVals(int x, int y) {
    w = x;
    h = y;
}

int rectangle::area() {
    return w * h;
}

int rectangle::perimeter() {
    return (2 * w) + (2 * h);
}

/*----------------CIRCLE CLASS----------------*/
class circle {
    const static double pi; // Declare static constant pi
    double r;
public: // Added public access specifier for these functions
    void setR(double);
    double area();
    double circumference();
};

// Define the static constant outside the class
const double circle::pi = 3.14159265358979;

void circle::setR(double radius) {
    r = radius;
}

double circle::area() {
    return pi * (r * r);
}

double circle::circumference() {
    return 2 * pi * r;
}

/*----------------MAIN FUNCTION----------------*/
int main() {
    int choice;
    cout << "Enter 1 if you would like to calculate the area of a rectangle, Enter 2 if you would like calculate the area of a circle\n";
    cin >> choice;

    if (choice == 1) {
        int width, height;
        cout << "Enter the width of the rectangle ";
        cin >> width;
        cout << "Enter the height of the rectangle ";
        cin >> height;

        rectangle r;
        r.setVals(width, height);
        cout << "The area of the rectangle is " << r.area() << "\n";
        cout << "The perimeter of the rectangle is " << r.perimeter() << "\n";
    } else if (choice == 2) {
        double r;
        cout << "Enter the radius of the circle ";
        cin >> r;

        circle c;
        c.setR(r);
        // Fixed syntax errors here
        cout << "The area of the circle is " << c.area() << "\n";
        cout << "The circumference of the circle is " << c.circumference() << "\n";
    }

    return 0;
}

Explanation of Changes

  1. Public Access for Circle Functions: Added the public: specifier above setR, area, and circumference in the Circle class, allowing main() to call these methods directly.
  2. Fixed Syntax Error: Removed the extra semicolon after c.area() and corrected the mistyped newline to "\n"; to resolve the compilation failure.
  3. Static Constant Pi: Changed pi to a const static member, defined outside the class. This ensures only one copy of pi exists in memory, which is more efficient and aligns with best practices for constant values shared across all class instances.

内容的提问来源于stack exchange,提问作者lazy-zebra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:05:17