New to classes

This is a discussion on New to classes within the C++ Programming forums, part of the General Programming Boards category; I've looked over this, and never having used classes before am a bit confused on what I'm doing wrong. If ...

  1. #1
    C noobie
    Join Date
    Jun 2006
    Location
    MI
    Posts
    25

    New to classes

    I've looked over this, and never having used classes before am a bit confused on what I'm doing wrong. If i initialize the counter variable prior to main as part of the class declaration it works fine, otherwise it gives me errors.

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    
    class time
    {
       private:
          int hours;
          int minutes;
          int seconds;
       public:
          time( int = 0 , int = 0 , int = 0 ); 
          void show_time();
          void tick_time();
    };
    
    time::time( int hr , int min , int sec )
    {
       hours = hr;
       minutes = min;
       seconds = sec;
    }
    
    void time::show_time()
    {
       cout << hours << ':' << minutes << ':' << seconds << '\n';
    
       return;
    }
    void time::tick_time()
    {
       ++seconds;
       if( seconds == 60 )
       {
          seconds = 0;
          ++minutes;
          
          if( minutes == 60 )
          {
             minutes = 0;
             ++hours;
          }
       }
       return;
    }
    
    
    
    
    
    int main()
    {
       time counter;
       counter.show_time();
       counter.tick_time();
       counter.show_time();
    
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    42
    I don't really understand your question. Mind to show us an example where it doesn't work?

  3. #3
    C noobie
    Join Date
    Jun 2006
    Location
    MI
    Posts
    25
    It doesn't compile for G++ but if you try it on codepad it will give an identical error message.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    42
    I don't really understand what you're saying. That code compiles (and works) fine...

  5. #5
    C noobie
    Join Date
    Jun 2006
    Location
    MI
    Posts
    25
    Did you try it on the site I gave? If you do it on Windows it may not give this error. Anyways, I fixed the issue by simply changing the name of the class. Seems my prof (who works under windows) told us to use time which is being used elsewhere, perhaps in IOSTREAM I don't know. Here's the error message if anyone is curious,

    In function 'int main()':
    Line 55: error: expected `;' before 'counter'
    compilation terminated due to -Wfatal-errors.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    There's this time, so with compilers that happen to include <time.h> from <iostream> you'd get a conflict. This being a standard name and hence in std namespace doesn't seem to help either, since apparently C functions are also available outside the namespace. So the only thing to do is to rename your class (a convention that type names begin with upper-case characters should avoid naming conflicts with the standard) or to put it in its own namespace.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Always learning
    Join Date
    Apr 2009
    Posts
    25
    it's funny, I had the exact same problem the first time I tried to learn how to use classes! I suggest you follow the convention mentioned by anon, which is to use the upper-case character for type names (eg Time, LinkedList) - not only it keeps this kind of error away, but your code will look better and easier to understand (at least to me :P)
    Last edited by Mr.Pointer; 04-20-2009 at 06:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21