problems compiling a month class

This is a discussion on problems compiling a month class within the C++ Programming forums, part of the General Programming Boards category; Hey I could really use somebody's help in explaining what I did wrong to get compiler errors with a "month" ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    6

    problems compiling a month class

    Hey I could really use somebody's help in explaining what I did wrong to get compiler errors with a "month" class I wrote for school. I am new to C++ so it's probably just some silly error or I am trying to do something completely wrong. I am just trying to get this thing to compile so you needn't debug it. Anyway all help will be greatly appreciated. EDIT: Many of my errors concern the declaration of monthAbbrev, does anyone see what I am doing wrong with it?

    Code:
    #ifndef MONTH_H
    #define MONTH_H
    
    const short MAX_M = 12;
    const short MAX_N = 4;
    
    class Month
    {
         short monthNum;
         const char monthAbbrev[MAX_M][MAX_N] = 
         {
              "jan", "feb", "mar", "apr", "may", 
              "jun", "jul", "aug", "sep", "oct", 
              "nov", "dec"
         };
         void translate(const char[]);
    
    public:
         Month();
         Month(const char name[]);
         Month(const short number);
         void input(void);
         void outputNumber(void) const;
         void outputName(void) const;
         Month nextMonth(void) const;
         bool compare(const char str1[], const char str2[]) const;
         short get_monthNum(void) const { return monthNum; }
         void set_monthNum(const short num) { monthNum = num; }
    };
    
    #endif
    And the implementation
    Code:
    #include <iostream>
    #include <cstring>
    #include <cctype>
    using namespace std;
    #include "month.h"
    
    Month::Month()
    {
         monthNum = 1;
    }
    Month::Month(const char name[])
    {
          translate(name);
    }	
    Month::Month(const short number)
    {
         monthNum = number;
    }
    void Month::input(void)
    {
         char name[MAX_N];
         cin >> monthNum;
         if(cin.fail()) //if fail must be a char
         {
              cin.clear();
              cin.getline(name, MAX_N); //get string
              name[MAX_N - 1] = '\0';
              translate(name); //translate name to set monthNum
         } 
         return;
    }
    void Month::outputNumber(void) const
    {
         cout << monthNum;
         return;
    }
    void Month::outputName(void) const
    {
         cout << monthAbbrev[monthNum - 1];
         return;
    }
    Month Month::nextMonth(void) const
    {
         Month temp(monthNum + 1);
         return temp;
    }
    bool Month::compare(const char str1[], const char str2[]) const
    {
         bool same = false;
         short index = 0;
         while(index != (MAX_N) && !same)
         {
              if(tolower(str1[index]) == tolower(str2[index]))
              {
    	same = true;
    	while(index != (MAX_N) && same)
    	{
    	     if(tolower(str1[index]) != tolower(str2[index]))
    	     {
    	          same = false;
    	     }
    	     index++;
    	}
                }
          }
         return same;
    }
    void Month::translate(const char[] name)
    {
         for(short i = 0; i != MAX_M; i++)
         {
              if(compare(monthAbbrev[i], name))
              {
    	monthNum = i;
              }
         }
         return;
    }
    I'm new to this forum also, so I hope I posted that code correctly and didn't break any of the forum's rules. BTW I posted all the code so you could maybe compile it and tell me what errors you get.
    Last edited by kkurz; 09-20-2003 at 06:01 PM.

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Ever thought putting these under private?

    Code:
         short monthNum;
         const char monthAbbrev[MAX_M][MAX_N] = 
         {
              "jan", "feb", "mar", "apr", "may", 
              "jun", "jul", "aug", "sep", "oct", 
              "nov", "dec"
         };
         void translate(const char[]);
    About the problem that you got, I think it's because you can't initialize monthAbbrev in header file.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,640
    First of all, you have to declare the array as static const char. Next, look at this bit of code:

    monthNum = number;

    What then happens when the user enters 300023, and then the code executes:

    cout << monthAbbrev[monthNum - 1];

    Finally, why are you putting a string comparison function in a class? That makes no sense. Just use strcasecmp(s1, s2)[located in string.h], the function returns 0 if they match. If you can't/don't want to use that, at *least* make the function outside of the class.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696

    monthNum = number;

    What then happens when the user enters 300023, and then the code executes:

    cout << monthAbbrev[monthNum - 1];
    Yup, input validation is important.


    Finally, why are you putting a string comparison function in a class? That makes no sense. Just use strcasecmp(s1, s2)[located in string.h], the function returns 0 if they match. If you can't/don't want to use that, at *least* make the function outside of the class.
    You know what, many people never realize that built-in functions are there until someone mentions about them

    I have a question thou,
    why static const char?

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,640
    You can't initialize variables within a class in the declaration:

    Code:
    struct foo {
    int i = 0;
    };
    That obviously won't compile.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    6
    Thanks for you replies so far, they have helped. As for strcasecmp, yeah I didn't know if there was a case insensitive compare function in cstring, so I wrote one real quick. Also, I didn't write any bounds checking because, well I wasn't sure exactly how do handle it. If it is out of bounds should I just default to say january's monthNum and cout a message saying "month out of bounds"? I always put my error checking where I actually prompt the user so I can loop the prompt until they enter valid data. I don't know, anymore comments are more than welcome.

    EDIT:

    Alright so I deleted my compare function and added strcasecmp to translate so its body now looks like this
    Code:
    void Month::translate(const char[] name)
    { 
         bool success = false;
         for(short i = 0; i != MAX_M; i++)
         {
              if(!strcasecmp(MONTHS[i], name))
              {
                    monthNum = i;
                    success = true;
              }
         }
         if(!success)
         {
              monthNum = 1;
         }
         return;
    }
    Also I placed monthAbbrev as a global constant and renamed it
    const char MONTHS[MAX_M][MAX_N] = ...
    anyway if I call it static const as suggested will I still be able to access it in the implementation file? As for some of the other functions, I added bounds checking with a default of monthNum = 1 if out of bounds occurs. I also changed nextMonth so it will work properly (ie if monthNum = 12 it now resets to 1). Long story short I think I fixed everything that was mentioned so far, but now my compiler (VS.net if that matters) doesn't like the translate function as I get many syntax errors with it. I would like to keep it a private function, so is there a different way to declare and define private functions? I thought they were implemented the same way as public functions. Thanks again for your time.
    Last edited by kkurz; 09-21-2003 at 08:24 AM.

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    6
    Nevermind I got it, what a stupid error I was making. Sometimes I just have to walk away and come back and it is all so glaringly obvious. Thanks again for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 10:27 PM
  3. Problems compiling two CPPs
    By EVIL_Murasame in forum C++ Programming
    Replies: 3
    Last Post: 11-03-2002, 10:29 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. Template problems
    By VirtualAce in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2002, 11:11 PM

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