ThePeter
ThePeter

Reputation: 33

Multiple Definitions compiling errors

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:

The following are related to implementation of the above files:

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.

Upvotes: 0

Views: 156

Answers (1)

M.M
M.M

Reputation: 141544

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.

Upvotes: 1

Related Questions