【发布时间】:2014-04-12 00:59:58
【问题描述】:
我在 StackExchange 上的第一篇文章! 我有一个 C++ 课程的作业;使用先前分配的日期类(月、日、年)和时间类(小时、分钟、上午/下午)进行约会类。 我想我已经解决了大部分主要/语法错误。
我的问题是,就我目前完成#includes 和头文件的方式而言,我得到一个多重定义错误 日期和时间的构造函数。(而且我对模板了解不多,但我必须使用它们。)
我的文件:
-
约会.cpp
#include "time.cpp" #include "date.cpp" #include "appointment.h"我需要能够创建时间/日期对象,我应该使用 .h 还是 .cpp 文件?
-
约会.h
#ifndef _APPOINTMENT_H_ #define _APPOINTMENT_H_ #include <iostream> #include "time.h" #include "date.h" -
日期.cpp
#ifndef _DATE_CPP_ #define _DATE_CPP_ #include "date.h" -
日期.h
#ifndef _DATE_H_ #define _DATE_H_ -
时间.cpp
#ifndef _TIME_CPP_ #define _TIME_CPP_ #include "time.h" -
时间.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; } -
读取.cpp
#include "arrayListType.h" #include "appointment.h" #include <fstream> using namespace std; void read(arrayListType<Appointment>& appointments) {...} -
输出.cpp
#include "arrayListType.h" #include "appointment.h" #include <iostream> #include <iomanip> using namespace std; void output(const arrayListType<Appointment>& appointments) {...} arrayListType.h(其中包含所有实现,作为模板)
- itemType.h
不确定您是否需要查看最后 2 个。如果我需要发布更多信息,我很高兴。我也有所有文件的压缩版本。
【问题讨论】:
-
你正在使用reserved identifiers。
-
只包含头文件而不包含源单元
-
我没有看到每个头文件各自的结束
#endif。他们在场吗? -
是的,我保留了#endif (s) 只是为了使帖子尽可能简短。
-
删除
.cpp包含现在会发生什么?
标签: c++ compiler-errors multiple-definition-error