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.

10
  • You're using reserved identifiers. Commented Apr 12, 2014 at 1:01
  • 4
    Only include header files not source units Commented Apr 12, 2014 at 1:02
  • I do not see the respective closing #endif for each header file. Are they present? Commented Apr 12, 2014 at 1:06
  • Yeah, I kept out the #endif (s) just to keep the post as short as possible. Commented Apr 12, 2014 at 1:08
  • What happens now with the removal of the .cpp inclusions? Commented Apr 12, 2014 at 1:10

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.