-4
    #include <iostream>
    using namespace std;

    class time{
        int date,month,year;
    public:
       void gettime(){
            cout<<"enter the (date/month/year)\n";
            cin>>date>>month>>year;
        }
        void show(){
            cout<<"Your age is:-"<<date<<month<<year;
        }
        friend time Add(time a1,time a2);
    };

    time Add(time a1,time a2){
            time temp;
            if(a2.date<a1.date)
            {
                a2.date=a2.date+30;
                temp.date=a2.date-a1.date;
                a2.month=a2.month-1;

            }
            else
                temp.date=a2.date-a1.date;
            if(a2.month<a1.month)
            {
                a2.month=a2.month+12;
                temp.month=a2.month-a1.month;
                a2.year=a2.year-1;
            }
            else
                temp.month=a2.month-a1.month;
            temp.year=a2.year-a1.year;
            return (temp);
    }

    int main()
    {
        time a1,a2,t3;
        a1.gettime();
        a2.gettime();
        t3=Add(a1,a2); //this is the friend function
        t3.show();
        return 0;
    }

This is working in Dev c++ but not in gcc and any other compiler.

age.cpp:17:1: error: ‘time’ does not name a type
 time Add(time a1,time a2){
 ^
age.cpp: In function ‘int main()’:
age.cpp:42:7: error: expected ‘;’ before ‘a1’
  time a1,a2,t3;
       ^
age.cpp:43:2: error: ‘a1’ was not declared in this scope
  a1.gettime();
  ^
age.cpp:44:2: error: ‘a2’ was not declared in this scope
  a2.gettime();
  ^
age.cpp:45:2: error: ‘t3’ was not declared in this scope
  t3=Add(a1,a2);
  ^
age.cpp:45:14: error: ‘Add’ was not declared in this scope
  t3=Add(a1,a2);
              ^
Fred Larson
  • 60,987
  • 18
  • 112
  • 174
Kunal Saini
  • 138
  • 10

1 Answers1

0

iostream is pulling in time.h or ctime. This is declaring the function time_t time(time_t *t); in the global namespace and/or std namespace, causing a name collision.

To resolve this, rename your class, or define it in your own namespace.

I also recommend staying away from using namespace std; to avoid other name collisions or ambiguities, although it is not the cause of your problem in this case.

Community
  • 1
  • 1
Fred Larson
  • 60,987
  • 18
  • 112
  • 174