简体   繁体   English

多个定义编译错误

[英]Multiple Definitions compiling errors

My first post on StackExchange! 我在StackExchange上的第一篇文章! I have an assignment for my C++ course; 我有一份C ++课程的作业; 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). 制作一个约会类,该约会类使用Date类(月,日,年)和Time类(小时,分钟,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. 我的问题是,随着我目前如何完成#includes和头文件,我得到了Date和Time构造函数多个定义错误 (And I don't know much about Templates, but I'm required to work with them.) (而且我对模板了解不多,但是我需要使用它们。)

My files: 我的文件:

  • Appointment.cpp 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? 我需要能够创建时间/日期对象,我应该使用.h还是.cpp文件?

  • Appointment.h Appointment.h

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

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

     #ifndef _DATE_H_ #define _DATE_H_ 
  • time.cpp time.cpp

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

     #ifndef _TIME_H_ #define _TIME_H_ 

The following are related to implementation of the above files: 以下与上述文件的实现有关:

  • main.cpp 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 read.cpp

     #include "arrayListType.h" #include "appointment.h" #include <fstream> using namespace std; void read(arrayListType<Appointment>& appointments) {...} 
  • output.cpp 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) arrayListType.h(以模板的形式包含所有实现)

  • itemType.h itemType.h

Not sure if you need to see the last 2. If I need to post more information, I'm glad to. 不知道您是否需要查看最后2个。如果我需要发布更多信息,我很高兴。 I have a compressed version of all the files too. 我也有所有文件的压缩版本。

Remove these lines from Appointment.cpp: 从Appointment.cpp中删除以下行:

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

You should almost never include a .cpp file from another one. 您几乎永远不应包含另一个文件中的.cpp文件。 As such, you can also remove the include guards that you have in your .cpp files, since you won't be including them. 因此,您也可以删除.cpp文件中的包含保护,因为您将不包括它们。

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: 这些来自main.cpp行必须位于一个头文件中,该文件包含在main.cpp和实现该功能的.cpp文件中:

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; 一旦调用了函数, .cpp文件实际上就可以完成工作。 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; "time.h"更改为其他内容; 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 有一个名为<time.h>的标准头,很容易使您的编译器或系统环境设置有误,并最终包含错误的头
  • use the format H_APPOINTMENT for the header guard token. 使用格式H_APPOINTMENT作为标头保护令牌。 Identifiers starting with _ followed by a capital letter are reserved; 保留以_开头,大写字母的标识符; as are all-caps identifiers starting with E . E开头的全大写字母标识符也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM