0

My first post on StackExchange! I have an assignment for my C++ course; to make an Appointment Class that uses a previous assignment of a Date class (month, day, year) and a Time class (Hour, Minute, AM/PM). I think I have most of the primary/syntax errors out of the way.

My problem is that with how I've currently done the #includes and header files, I get a multiple definitions error of the constructors for Date and Time. (And I don't know much about Templates, but I'm required to work with them.)

My files:

  • Appointment.cpp

    #include "time.cpp"
    #include "date.cpp"
    #include "appointment.h"
    

    I need to be able to create a Time/Date object, should I use .h or .cpp files?

  • Appointment.h

    #ifndef _APPOINTMENT_H_
    #define _APPOINTMENT_H_
    #include <iostream>
    #include "time.h"
    #include "date.h"
    
  • date.cpp

    #ifndef _DATE_CPP_
    #define _DATE_CPP_
    #include "date.h"
    
  • date.h

    #ifndef _DATE_H_
    #define _DATE_H_
    
  • time.cpp

    #ifndef _TIME_CPP_
    #define _TIME_CPP_
    #include "time.h"
    
  • time.h

    #ifndef _TIME_H_
    #define _TIME_H_
    

The following are related to implementation of the above files:

  • main.cpp

    #include "arrayListType.h"
    #include "appointment.h"
    
    void read(arrayListType<Appointment>&);
    void output(const arrayListType<Appointment>&);
    
    int main()
    {
       arrayListType<Appointment> appointments;
       read(appointments);
       output(appointments);
       return 0;
    }
    
  • read.cpp

    #include "arrayListType.h"
    #include "appointment.h"
    #include <fstream>
    using namespace std;
    
    void read(arrayListType<Appointment>& appointments)
    {...}
    
  • output.cpp

    #include "arrayListType.h"
    #include "appointment.h"
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    void output(const arrayListType<Appointment>& appointments)
    {...}
    
  • arrayListType.h (which has all of the implementation in, as templates)

  • itemType.h

Not sure if you need to see the last 2. If I need to post more information, I'm glad to. I have a compressed version of all the files too.

Danvil
  • 22,240
  • 19
  • 65
  • 88
ThePeter
  • 33
  • 1
  • 7
  • You're using [reserved identifiers](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – chris Apr 12 '14 at 01:01
  • 4
    Only include header files not source units – johnbakers Apr 12 '14 at 01:02
  • I do not see the respective closing `#endif` for each header file. Are they present? – CPlusPlus OOA and D Apr 12 '14 at 01:06
  • Yeah, I kept out the #endif (s) just to keep the post as short as possible. – ThePeter Apr 12 '14 at 01:08
  • What happens now with the removal of the `.cpp` inclusions? – CPlusPlus OOA and D Apr 12 '14 at 01:10
  • @OpenLearner, if I use the header files, then how will the header file access the implementation of it? – ThePeter Apr 12 '14 at 01:11
  • Perhaps some review material about the C++ compiler and linker will help. There must be something that is in your book clarifying how the pre-processor does its work, then compiling, then linking per directions in a makefile. Keep in mind that a makefile is optional. All of the compiling and linking commands can be individually executed at the command line. – CPlusPlus OOA and D Apr 12 '14 at 01:13
  • @CPlusPlusOOAandD, linking files is nothing new to our class. Been doing it for awhile, but I just don't see the connection from one .cpp class that #includes date.h to be able to create a object without being able to access date.cpp – ThePeter Apr 12 '14 at 01:15
  • Well, the short answer is that including the header file is necessary in order for a `.cpp` file to know enough class signature details to correctly obtain the actual details in the linker stage. This should already have been discussed as part of the C and C++ basics in the course and book. – CPlusPlus OOA and D Apr 12 '14 at 01:18
  • I agree that you need to review some basics on how C++ compilation and linking happens. Will make your life much easier if you actually understand what they are doing, and a single question on stack overflow is not likely going to resolve the confusion for you. – johnbakers Apr 12 '14 at 02:11

1 Answers1

1

Remove these lines from Appointment.cpp:

#include "time.cpp"
#include "date.cpp"

You should almost never include a .cpp file from another one. As such, you can also remove the include guards that you have in your .cpp files, since you won't be including them.

These lines from main.cpp need to be in a header file which is included from main.cpp and from the .cpp file that implements the function:

void read(arrayListType<Appointment>&);
void output(const arrayListType<Appointment>&);

You seem to be missing the point of header files. The idea is to separate interface and implementation. The header file provides everything that a different unit needs to know in order to be able to call the functions listed in the header. The .cpp file actually does the work once the functions have been called; and the other units don't need to know how that works , just so long as it meets the "contract" specified by the header file.

I'd also suggest some more changes to avoid possible clashes:

  • Change "time.h" to something else; there's a standard header called <time.h> and it's easy to have your compiler or system environment set up slightly wrong and end up including the wrong one
  • use the format H_APPOINTMENT for the header guard token. Identifiers starting with _ followed by a capital letter are reserved; as are all-caps identifiers starting with E.
M.M
  • 138,810
  • 21
  • 208
  • 365