why the code is not running?

#include<iostream>
#include<string.h>
using namespace std;
class person
{
    char name[20];
    float age;
    public:
        person(char *s,float a)
        {
            strcpy(name,s);
            age=a;
        }
        person & person::greater(person &x)
        {
            if(x.age>=age)
                return x;
            else
                    return *this;
        }
        void display(void)
        {
            cout<<"Name:"<<name<<"\n"<<"Age:"<<age<<"\n";
        }
         int main()
        {
            person P1("John",37.50),P2("Ahmed",29.0),P3("Hebber",40.25);
            person P('\0',o);
            P=P1.greater(P3);
            cout<<"Elder person is: \n";
            P=P1.greater(P2);
            cout<<"Elder person is: \n";
            P.display();
        }
}

Recommended Answers

All 8 Replies

Your main function needs to be outside of your class defenition

what is the problem in this line?

person & person::greater(person &x)

Did you move main outside of the class? If you have post the code you have know.

#include<iostream>
#include<string.h>
using namespace std;
class person
{
    char name[20];
    float age;
    public:
        person(char *s,float a)
        {
            strcpy(name,s);
            age=a;
        }
        person & person::greater(person &x)
        {
            if(x.age>=age)
                return x;
            else
                    return *this;
        }
        void display(void)
        {
            cout<<"Name:"<<name<<"\n"<<"Age:"<<age<<"\n";
        }
}
         int main()
        {
            person P1("John",37.50),P2("Ahmed",29.0),P3("Hebber",40.25);
            person P('\0',o);
            P=P1.greater(P3);
            cout<<"Elder person is: \n";
            P=P1.greater(P2);
            cout<<"Elder person is: \n";
            P.display();
        }

You need a semi colon after the closing brace on line 25

still its showing error in line 14 & 29

Line 14:

        person & person::greater(person &x)

it's incorrect. Remove the person:: part. The class scope is used only when implementing functions outside the class, where they were define (such as in a .h and .cpp file: in the header you declare the functions, but in the .cpp, you implement them, so you'll need there to put the class scope operator, so that it would know that you're implementing that classes functions).

Line 29:

            person P('\0',o);

what is o suppose to be? Your custom constructor of the class takes a pointer to a char array (string) and a float. In your case you provide a single character, which as it is, it would take it's address, and some symbol o, which is not implemented/defined anywhere. Perhaps you wanted to put there the number 0. Also, when asking for strings (pointers to char arrays) use double quotes, "", to declare them, not single quotes, '', which defines characters.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.