In main (), not specified in scope

When compiling the below code, I am getting the error,

in int main (), t1 was not declared in this scope.

I am using g++

. In main()

I have already announced t1

, t2

and t3

. Then why am I getting this error?

#include<iostream>
using namespace std;
class time
{
    int hours;
    int minute;
public:
    void getdata(int h,int m)
    {
    hours=h;
    minute=m;
    }   
    void putdata(void)
    {   
        cout<<"\n Hours = "<<hours;
        cout<<"\n Minutes = "<<minute;
    }   
    void sumtime(time ,time);

};
void time::sumtime(time t1,time t2)
{ 
    minute=t1.minute+t2.minute;
    hours=minute/60;
    minute=minute%60;
    hours = hours + t1.hours + t2.hours;
}

int main()
{
    time t1,t2,t3;
    t1.getdata(2,45);
    t2.getdata(3,30);
    t3.sumtime(t1,t2);
    cout<<"\n T1 ... "<<t1.putdata();
    cout<<"\n T2 ..."<<t2.putdata();
    cout<<"\n T3 ... "<<t3.putdata();

    return 0; 
}

      

+3


source to share


3 answers


Your named class time

most likely collides with the name C Standard Library Function of the same name .

My recommendation would be to put your class and related functions in their own namespace.



I also recommend that you not using namespace std

, but instead just bring the things you really need, for example using std::cout

. Better yet, avoid using

altogether and just specify in your code.

+7


source


There is one bug in this program. In functionmain()

cout<<"\n T1 ... "<<t1.putdata()

      



for the above line putdata()

will return the type void

, but cout

need char

or the type to be converted. So it should be something like:

cout<<"\n T3 ... ";
t3.putdata();

      

0


source


Try renaming your class to Time

(not Time

) because the time class already exists in the std namespace.

-2


source







All Articles