Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 多定义编译错误_C++_Compiler Errors_Multiple Definition Error - Fatal编程技术网

C++ 多定义编译错误

C++ 多定义编译错误,c++,compiler-errors,multiple-definition-error,C++,Compiler Errors,Multiple Definition Error,我在StackExchange上的第一篇帖子! 我有一个任务,为我的C++课程;使用日期类(月、日、年)和时间类(小时、分钟、上午/下午)的先前分配进行预约。 我想我已经排除了大多数主要/语法错误 我的问题是,在我目前处理#includes和头文件的方式中,我得到了日期和时间构造函数的多个定义错误。(我对模板了解不多,但我需要使用它们。) 我的文件: Appointment.cpp #include "time.cpp" #include "date.cpp" #include "appoin

我在StackExchange上的第一篇帖子! 我有一个任务,为我的C++课程;使用日期类(月、日、年)和时间类(小时、分钟、上午/下午)的先前分配进行预约。 我想我已经排除了大多数主要/语法错误

我的问题是,在我目前处理#includes和头文件的方式中,我得到了日期和时间构造函数的多个定义错误。(我对模板了解不多,但我需要使用它们。)

我的文件:

  • Appointment.cpp

    #include "time.cpp"
    #include "date.cpp"
    #include "appointment.h"
    
    我需要能够创建时间/日期对象,我应该使用.h还是.cpp文件

  • 预约

    #ifndef _APPOINTMENT_H_
    #define _APPOINTMENT_H_
    #include <iostream>
    #include "time.h"
    #include "date.h"
    
  • 日期:h

    #ifndef _DATE_H_
    #define _DATE_H_
    
  • time.cpp

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

    #ifndef _TIME_H_
    #define _TIME_H_
    
以下内容与上述文件的实施有关:

  • 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;
    }
    
    #包括“arrayListType.h”
    #包括“约会.h”
    无效读取(arrayListType&);
    无效输出(const arrayListType&);
    int main()
    {
    arrayListType任命;
    宣读(任命);
    产出(任命);
    返回0;
    }
    
  • read.cpp

    #include "arrayListType.h"
    #include "appointment.h"
    #include <fstream>
    using namespace std;
    
    void read(arrayListType<Appointment>& appointments)
    {...}
    
    #包括“arrayListType.h”
    #包括“约会.h”
    #包括
    使用名称空间std;
    无效读取(ArrayList类型和约会)
    {...}
    
  • output.cpp

    #include "arrayListType.h"
    #include "appointment.h"
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    void output(const arrayListType<Appointment>& appointments)
    {...}
    
    #包括“arrayListType.h”
    #包括“约会.h”
    #包括
    #包括
    使用名称空间std;
    无效输出(常量arrayListType和约会)
    {...}
    
  • arrayListType.h(以模板形式在中包含所有实现)

  • itemType.h

不确定是否需要查看最后2个。如果我需要发布更多信息,我很乐意。我也有所有文件的压缩版本

从Appointment.cpp中删除以下行:

#include "time.cpp"
#include "date.cpp"
您几乎不应该包含来自另一个文件的
.cpp
文件。因此,您还可以删除
.cpp
文件中的include-guard,因为您不会将它们包括在内

main.cpp
中的这些行需要位于头文件中,该头文件包含在
main.cpp
和实现该功能的
.cpp
文件中:

void read(arrayListType<Appointment>&);
void output(const arrayListType<Appointment>&);
void read(arrayListType&);
无效输出(const arrayListType&);
您似乎忽略了头文件的要点。其思想是分离接口和实现。头文件提供了不同单元需要知道的所有信息,以便能够调用头文件中列出的函数。调用函数后,
.cpp
文件实际上完成了工作;其他单位不需要知道它是如何工作的,只要它满足头文件指定的“契约”

我还建议进行更多更改,以避免可能的冲突:

  • “time.h”
    更改为其他内容;有一个名为
    的标准头,很容易使编译器或系统环境设置稍有错误,最终包含错误的头
  • 使用头保护令牌的格式
    H_APPOINTMENT
    。保留以
    开头的标识符,后跟大写字母;所有以
    E
    开头的caps标识符也是如此

您正在使用。仅包括头文件而非源单位。我看不到每个头文件的相应关闭
#endif
。他们在场吗?是的,我保留了#endif(s)只是为了让帖子尽可能短。删除
.cpp
包含内容后会发生什么?
void read(arrayListType<Appointment>&);
void output(const arrayListType<Appointment>&);