1
#include<iostream>
using namespace std;

class circle
{
public:
    int r;
    getr()
    {
        cout<<"enter radius";
        cin>>r;
    }
    area()
    {
        cout<<"area is "<<(3.14*r*r);
    }
}

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

Im getting the following errors:

g++ /tmp/E854sHhnHj.cpp /tmp/E854sHhnHj.cpp:8:10: error: ISO C++ forbids declaration of 'getr' with no type [-fpermissive] 8 | getr() | ^

/tmp/E854sHhnHj.cpp:13:10: error: ISO C++ forbids declaration of 'area' with no type [-fpermissive] 13 | area() | ^

/tmp/E854sHhnHj.cpp:17:2: error: expected ';' after class definition 17 | } | ^ | ; /tmp/E854sHhnHj.cpp: In member function 'int circle::getr()':

/tmp/E854sHhnHj.cpp:12:5: warning: no return statement in function returning non-void [-Wreturn-type] 12 | } | ^

/tmp/E854sHhnHj.cpp: In member function 'int circle::area()': /tmp/E854sHhnHj.cpp:16:5: warning: no return statement in function returning non-void [-Wreturn-type] 16 | } | ^

Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
Roshan
  • 43
  • 5

2 Answers2

3

You need to state the return types in your member function definitions:

void getr() { ... }
^^^^               
Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
  • i want it to ask for a radius and print the area... the void gives me a result like: $g++ -o main *.cpp $main enter radiusarea is 0 – Roshan Aug 19 '21 at 08:36
3

All functions in C++ (whether they are members of classes or free-standing) need a declared return type (before the function name). In your case, as the functions aren't returning anything, you should declare those return types as void.

Also, you're missing a semicolon after the class definition:

class circle {
public:
    int r;
    void getr() { // Need a return type for functions - "void" seems logical here
        cout << "enter radius";
        cin >> r;
    }
    void area() { // ... and again
        cout << "area is " << (3.14 * r * r);
    }
}; // Must have a semicolon here!
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • i want it to ask for a radius and print the area... the void gives me a result like: $g++ -o main *.cpp $main enter radiusarea is 0 – Roshan Aug 19 '21 at 08:39
  • 1
    @Roshan No the void does not give you anything like that. Something else does. The suggested fix [works](https://godbolt.org/z/exs5Mq5jo). – n. m. could be an AI Aug 19 '21 at 08:51
  • @n. 1.8e9-where's-my-share m. ohok now i see it, thanks. since u asked: im studying from my professor's notes and havent got a book as of now. – Roshan Aug 19 '21 at 09:00