c++ constructor related question

hi
I'm quite new to c++
I just write the following code :
code:
#include<iostream>
using namespace std ;
class time
{
int hour;
int min;
public: time(int a)
{
hour = a/60;
min = a%60 ;
}
};

int main()
{
int a =125;
time t1=a;
cout<< t1.hour ;
cout<<"\n";
cout<<t1.min;
}
I saved it as ti .cpp
I compiled it wit command :
g++ ti.cpp;
it shows following error :
ti.cpp: In function ‘int main()’:
ti.cpp:18: error: expected ‘;’ before ‘t’
ti.cpp:20: error: ‘t’ was not declared in this scope

I dont know what I'm doing wrong ;
how can we invoke constructor like ti=a; ? please explain me this concept
The only errors I get are that t1.hour/min are private member variables. Are you sure that is the code you are compiling?
I changed to public but same error and please tell me how, this constructor work
closed account (zb0S216C)
Maybe it's due to the fact that you're trying to initialize t1 with an integer. Call time's constructor. For example:

1
2
int a = 125;
time t1( a );

Actually, your error list specifies: error: expected ‘;’ before ‘t’. t is never defined in the code you posted.

Wazzak
Last edited on
1)main function should have a return type....probably in this case return 0
2)variables hour and min should be in public
3)try changing the class name..ie replace time by some other name...i am advising this because probably your compiler is calling the default time function...

i hope it works after all this changes

BEST OF LUCK
closed account (zb0S216C)
geekocoder wrote:
main function should have a return type....probably in this case return 0

If no returning value is specified, zero is returned by default.

Wazzak
yeah yeah....it just slipped out of my mind.....thanx framework
It was a problem of complier ?
the complier does not allow us to make class time .
every thing else was right . the data member should be public.
Topic archived. No new replies allowed.