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;
}
source to share
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.
source to share