C++编译错误求助:成员函数无返回类型、类定义语法问题等报错排查
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

