1
votes
#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 | } | ^

2
If you don't want to return something, you need to declare it: void getr()Damien
and put ';' after your class definitionLennert Antson
What C++ book are you reading?n. 1.8e9-where's-my-share m.
I am asking because there is a lot of junk C++ books out there, and we need to warn people against using them. You may benefit from a good one.n. 1.8e9-where's-my-share m.

2 Answers

3
votes

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

void getr() { ... }
^^^^               
3
votes

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!