Thread: Trying to connect together a header and two cpp files. Any help would be appreciated

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    12

    Exclamation Trying to connect together a header and two cpp files. Any help would be appreciated

    I had all of my code in working order when it was all just in one file. Once I tried to separate it into a header and another source file, it does not work anymore. I hope hoping that someone could help me out and steer me in the right direction with this. Thank you.

    Here is my Header file:
    Code:
        #ifndef DATE_H
        #define DATE_H
        
        #include <iostream>
        #include <iomanip>
        #include <string>
        using std::cout;
        using std::endl;
        using std::setfill;
        using std::setw;
        using std::string;
        
        class Date
        {
            
        
        
        public:
               
           // This is the default constructor
           Date(int mm = 1, int dd = 1, int yy = 2000)
           {    
                setMonth(mm);
                setDay(dd);
                setYear(yy);  
           };
        
            
           // Function that will set the month
           void setMonth(int mm);
           
           
           // Function that will set the day
           void setDay(int dd);
           
        
           // Function that will set the year       
           void setYear(int yy);
              
            // Function that will get the month    
           int getMonth();
           
           // Function that will get the day
           int getDay();
           
           // Function that will get the year
           int getYear();
           
           // Function that will get the name of the month when the right number is selected.
           std::string getMonthName();
            
           // Print function that outputs date in numeric format
           void print();
        
            //rint function that outputs date in long format.
           void printLong();
           
        
        private:
           
           int month;
           int day;
           int year;
        };
           
           
        
        
        #endif
    Here is my main.cpp:
    Code:
        #include <iostream>
        #include <iomanip>
        #include <string>
        #include "date.h"
        using std::cout;
        using std::endl;
        using std::setfill;
        using std::setw;
        using std::string;
        
        //#include "Date.h"
        
        // note - you may need to change the definition of the main function to
        // be consistent with what your C++ compiler expects.
        
        //int _tmain(int argc, _TCHAR* argv[])
        
        int main()
        {
            Date d1;             // default ctor
            Date d2(7, 4, 1976); // July 4'th 1976
            Date d3(0, 15, 1880);// Adjusted by ctor to January 15'th 1900
          
            d1.print();         // prints 01/01/2000
            d1.printLong();     // prints 1 January 2000
            cout << endl;
        
            d2.print();         // prints 07/04/1976
            d2.printLong();     // prints 4 July 1976
            cout << endl;
        
            d3.print();         // prints 01/15/1900
            d3.printLong();     // prints 15 January 1900
            cout << endl;
        
            cout << "object d2's day is " << d2.getDay() << endl; 
            cout << "object d2's month is "  << d2.getMonth() << " which is " << d2.getMonthName() << endl;
            cout << "object d2's year is " << d2.getYear() << endl;
        
            return 0;
        }
    and here is my source cpp:
    Code:
        #include "date.h"
         
         void setMonth(int mm)
           {
                   month = (mm);
           }
           
              void setDay(int dd)
           {
                day = (dd);
           }
           
              void setYear(int yy)
           {
                  year = (yy);
           }
           
            int getMonth()
           {
                 // If month is under 1 and over 12, then month = 1
              if (month < 1 || month > 12) 
              {
              month = 1;
              }
              return month; 
           } 
           
             int getDay()
           {    
           // If month equals 2 and day is greater than 28, then day = 1.
            if (month == 2 && day > 28)
                {
                day = 1 ;
                }
                
            // If the day is less than 1 and greater than 31 and is one of the listed months, then day = 1.
            if  ((day < 1 || day>31) && (month==1 || month ==3 || month == 5 || month == 7 || month== 8 || month== 10 || month==12))
                {
                day = 1 ;
                }
            // If the day is less than 1 and greater than 30 and is one of the listed months, then day = 1.
            if  ((day < 1 || day>30) && (month==4 || month ==6 || month == 9 || month == 11))
                {
                day = 1 ;
                }
            return day;       
           } 
           
             int getYear()
           {
              if (year < 1900)
              year = 1900;
              
              return year;
           } 
           
           string getMonthName()
           {
            string MonthName;
                
                if (month == 1)
                MonthName = ("January");
                if (month == 2)
                MonthName = ("February");
                if (month == 3)
                MonthName = ("March");
                if (month == 4)
                MonthName = ("April");
                if (month == 5)
                MonthName = ("May");
                if (month == 6)
                MonthName = ("June");
                if (month == 7)
                MonthName = ("July");        
                if (month == 8 )
                MonthName = ("August");
                if (month == 9)
                MonthName = ("September");
                if (month == 10)
                MonthName = ("October");
                if (month == 11)
                MonthName = ("November");
                if (month == 12)
                MonthName = ("December");
                
                return MonthName;
            }
            
               void print() //
           {
              //Call the functions to output.
              cout << "The date is \n" << getMonth() << "/" << getDay() << "/" << getYear()<< endl;
           } 
           
              // Print function that outputs date in long format.
           void printLong()
           {
              // Calls the functions to output
              cout << "The date is \n"<< getDay() << " "  << getMonthName() << " " << getYear()<< endl;
           }
    please! anything would be amazing. THank you

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?

    By the way, do not place using declarations at file/namespace scope in a header file as the effect of the using declarations will extend into the source files in which it is included, which is usually unexpected.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    It's been about a day with no response from the OP.

    Well, in the interest of making a good thread in case this is searched up, I can see what is wrong. Remember that when you separate a class declaration from it's implementation, you have to indicate that the functions you are writing are in class scope. The compiler can tell the difference between a free function getDay() and a getDay() that belongs to Date:: .

    Here is what date.cpp would look like with Date:: functions, it's all you should need to get it to work.
    Code:
    #include "date.h"
      
    void Date::setMonth(int mm) { month = (mm); }
    
    void Date::setDay(int dd) { day = (dd); }
    
    void Date::setYear(int yy) { year = (yy); }
    
    int Date::getMonth() {
      // If month is under 1 and over 12, then month = 1
      if (month < 1 || month > 12) {
        month = 1;
      }
      return month;
    }
    
    int Date::getDay() {
      // If month equals 2 and day is greater than 28, then day = 1.
      if (month == 2 && day > 28) {
        day = 1;
      }
    
      // If the day is less than 1 and greater than 31 and is one of the listed
      // months, then day = 1.
      if ((day < 1 || day > 31) &&
          (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 ||
           month == 10 || month == 12)) {
        day = 1;
      }
      // If the day is less than 1 and greater than 30 and is one of the listed
      // months, then day = 1.
      if ((day < 1 || day > 30) &&
          (month == 4 || month == 6 || month == 9 || month == 11)) {
        day = 1;
      }
      return day;
    }
    
    int Date::getYear() {
      if (year < 1900)
        year = 1900;
    
      return year;
    }
    
    string Date::getMonthName() {
      string MonthName;
    
      if (month == 1)
        MonthName = ("January");
      if (month == 2)
        MonthName = ("February");
      if (month == 3)
        MonthName = ("March");
      if (month == 4)
        MonthName = ("April");
      if (month == 5)
        MonthName = ("May");
      if (month == 6)
        MonthName = ("June");
      if (month == 7)
        MonthName = ("July");
      if (month == 8)
        MonthName = ("August");
      if (month == 9)
        MonthName = ("September");
      if (month == 10)
        MonthName = ("October");
      if (month == 11)
        MonthName = ("November");
      if (month == 12)
        MonthName = ("December");
    
      return MonthName;
    }
    
    void Date::print() //
    {
      // Call the functions to output.
      cout << "The date is \n" << getMonth() << "/" << getDay() << "/" << getYear()
           << endl;
    }
    
    // Print function that outputs date in long format.
    void Date::printLong() {
      // Calls the functions to output
      cout << "The date is \n" << getDay() << " " << getMonthName() << " "
           << getYear() << endl;
    }

  4. #4
    Registered User
    Join Date
    Sep 2017
    Posts
    12
    Thanks you guys. I appreciate the help. I am now getting an error saying [Error] redefinition of of 'void Date:: etc.'

  5. #5
    Registered User
    Join Date
    Sep 2017
    Posts
    12
    I think I narrowed it down to my header file. Either my header guards arent working or something is duplicating something? I am not sure.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Please paste the complete errors instead of what you recall.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    In your header file, you define your constructor. When this header file gets included in two source files, the linker will think there's two copies of it. This is an error.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  8. #8
    Registered User
    Join Date
    Sep 2017
    Posts
    12
    turns out I had to have them in the same folder. Thanks everyone

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. about #include files and header files
    By bulletbutter in forum C++ Programming
    Replies: 9
    Last Post: 04-18-2008, 10:24 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. Replies: 4
    Last Post: 12-14-2005, 02:21 PM
  4. include library header in header files
    By Raison in forum C++ Programming
    Replies: 6
    Last Post: 09-27-2004, 02:50 AM
  5. header files and code files..
    By CompiledMonkey in forum C++ Programming
    Replies: 4
    Last Post: 02-15-2002, 09:35 AM

Tags for this Thread