Adi
Adi

Reputation: 99

in main() , Not Declared in Scope

On compiling the below code, I get the error,

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

I am using g++. In main() I have already declared 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; 
}

Upvotes: 3

Views: 1895

Answers (3)

Puneet Kumar
Puneet Kumar

Reputation: 1

There is one error in this program. In main() function

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

for above line putdata() will return void type but cout need char or convertible type. So it should be something like:

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

Upvotes: 0

vincentp
vincentp

Reputation: 1433

Try to rename your class to Time (not time), because the time class already exists in the namespace std.

Upvotes: -2

John Dibling
John Dibling

Reputation: 101456

Your class named time is likely name-colliding with the C Standard Library function of the same name.

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

I'd also recommend that you not using namespace std, but instead just bring in the things you actually need, such as using std::cout. Even better, avoid using altogether and just be explicit in your code.

Upvotes: 7

Related Questions