0

H, Here is my code that is giving error object is undeclared first use this function although every thing is alright, why is this happening so? that class is not visible to main function and its giving errors.

 #include <iostream.h>
 #include <stdio.h>
 #include <conio.h>
 using namespace std;
 class time
 {
  int min;
  int sec;
  int hour;

  public:
         time()
         {
               cout << "uninitialized constructor " << endl << endl;
               min = 0;
               sec = 0 ;
               hour = 0;
         }
         time(int m, int s, int h)
         {
                  min= m;
                  sec = s;
                  hour = h;
         }
         void display()
         {
              cout << min << ":" << sec <<":" << hour ;
         }
         void add(time t1, time t2)
         {
              int sece = t1.sec+t2.sec;
              int mint = t1.min + t2.min;
              int hours = t1.hour + t2.hour;

              time t4(sece , mint, hours);

         }
 };

 int main(void)
 {
  time t1(23, 45,11);
  time t2(11,59,59);
  time t3;
  t3.add(t1,t2);
  t3.display();
  getch();
 }

secondly, what does this code line means?

time(int h,int m,int s):hrs(h),mins(m),secs(s){}

could it be replaced by this: time(int m, int s, int h) { min= m; sec = s; hour = h; }

Arsala Kamal
  • 51
  • 1
  • 3
  • 9
  • ``? That should be ``, it's not the 90's anymore... (and see if it solves your error) – Daniel Frey Sep 14 '13 at 20:19
  • Whats the difference behind including or are these not alike? – Arsala Kamal Sep 14 '13 at 20:45
  • 1
    `iostream.h` is pre-C++98 and might have problems with namespaces. It is not officially standardized and not part of C++, so it should be avoided. One of the best sources for C++, cppreference.com, has a list of all [C++ headers](http://en.cppreference.com/w/cpp/header). – Daniel Frey Sep 14 '13 at 20:48

4 Answers4

2

show the exact error.

time(int h,int m,int s):hrs(h),mins(m),secs(s){}

That's an initializer list.

// could it be replaced by this:
time(int m, int s, int h) { min= m; sec = s; hour = h; } 

No, because you changed the argument order.

But time(int h,int m,int s):hrs(h),mins(m),secs(s){} is similar to

time(int h, int m, int s) { hour = h; min = m; secs = s; }
ninMonkey
  • 7,211
  • 8
  • 37
  • 66
1

Your choice of the class name is a bit unfortunate, since there is already a function named std::time in the standard library, so just choose a different name or consider using namespaces.

Concerning the second question:

This way of initializing the member variables is called initializer list and is the prefered way to use. Especially when dealing with objects instead of primitive data types, only the initializer list will make sure that your objects' constructors are called only once. Otherwise, the default constructor would be called implicitly, only for you to initialize the objects a second time inside the constructor body.

Community
  • 1
  • 1
ph4nt0m
  • 948
  • 4
  • 10
  • 23
0

As for your second question -

time(int h,int m,int s):hrs(h),mins(m),secs(s){}

This line uses MIL - Member initialization list, as for use/not use - Why should I prefer to use member initialization list?

With some small modifications, you can start using this.

please provide the compilation error for the first question, without seeing the error, i can see one main reason why build might fail. the language already defines a type named time, so you may want to change the name.

Community
  • 1
  • 1
user1708860
  • 1,683
  • 13
  • 32
0

Your code will not print correct output if you want t3 to really have the sum of t1,t2. If the code is actually what is required, then it is fine.

But, if add really needs to add, then don't create another local variable t4 in the function. Rather modify the member variables like this:

 void add(Time t1, Time t2)
 {
     sec = t1.sec+t2.sec;
     min = t1.min + t2.min;
     hour = t1.hour + t2.hour;
     min+=sec/60; sec%=60;
     hour+=min/60; min%=60;
 }
Abhinav
  • 1,496
  • 3
  • 15
  • 31