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

C++编译错误求助:成员函数无返回类型、类定义语法问题等报错排查

Fixing Your C++ Circle Area Calculation Code Issues

Let's walk through each compilation error and warning you're facing, then fix them step by step:

1. Missing Return Types for Class Member Functions

The errors saying "ISO C禁止声明无类型的‘getr’" and "ISO C禁止声明无类型的‘area’" occur because C++ mandates that all functions (including class member functions) must explicitly declare their return type. Your getr() and area() functions don't need to return any value, so you should mark them as void.

2. Missing Semicolon After Class Definition

The error "类定义后缺少‘;’" is a common oversight: in C++, every class definition must end with a semicolon right after the closing curly brace }.

3. No Return Statement Warnings

These warnings are a side effect of the first issue. Since you didn't specify a return type, the compiler defaulted to assuming int (an old C-style fallback), then complained when you didn't return an integer value. Once we set the return type to void, these warnings will vanish automatically.

Corrected Full Code

Here's the fixed version of your code with all issues resolved:

#include<iostream> 
using namespace std; 

class circle { 
public: 
    int r; 
    void getr() { 
        cout << "enter radius: "; 
        cin >> r; 
    } 
    void area() { 
        cout << "area is " << (3.14 * r * r) << endl; 
    } 
}; // Added required semicolon here

int main() { 
    circle one; 
    one.getr(); 
    one.area(); 
    return 0; 
}

Quick Extra Tip

I added a small formatting tweak to the output messages (a colon and newline) for better readability, but that's optional. If you need more precision for π instead of using 3.14, you can use M_PI from the <cmath> header—just note you might need to define _USE_MATH_DEFINES before including <cmath> on some compilers.

内容的提问来源于stack exchange,提问作者Roshan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 06:47:39