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();
        }
}
#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();
        }

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.

Edited 4 Years Ago by Lucaci Andrew

This question has already been answered. Start a new discussion instead.