为什么会出现下面的问题?
//Date.h
class Date
{
public :
Date(int = 1,int = 1,int = 1900);//默认构造函数
void setMonth(int); //私有成员设定
void setDay(int);
void setYear(int);
void print() const ; //以MONTH/DAY/YEAR格式输出
~Date(); //
private:
int month ;
int day ;
int year ;
int checkDay(int ) const ;
};
//Date.cpp
#include <iostream>
using std::cout;
using std::endl;
#include "Date.h"
Date::Date(int mn, int dy, int yr)
{
setMonth(mn);
setDay(dy);
setYear(yr);
cout << "Date object constructor for date";
print() ;
cout <<endl;
}
void Date::setDay(int dy)
{
day = checkDay(dy);
}
void Date::setYear(int yr)
{
year = yr;
}
void Date::setMonth(int mn)
{
if(mn > 0 && mn <=12)
{
month = mn ;
}
else
{
month = 1;
cout << "Invalid month (" << mn << ") set to 1. \n ";
}
}
void Date::print() const
{
cout << month << '/' <<day <<'/' <<year;
}
Date::~Date()
{
cout << "Date object destructor for date ";
print();
cout << endl;
}
int Date::checkDay(int testDay) const
{
static const int daysPerMonth[13]=
{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(testDay > 0 && testDay <= daysPerMonth[month])
return testDay;
if(month == 2 && testDay == 29 && (year % 400 ==0 || (year % 4 == 0 && year % 100 == 0)))
return testDay;
cout << " Invalid day (" << testDay << ") set to 1 .\n";
return 1;
}
//Employee.h
#include "Date.cpp"
class Employee
{
public :
Employee(const char * const ,const char *const
,const Date &, const Date &);
void print() const ;
~Employee();
private:
char firstName[25];
char lastName[25];
const Date birthDate;
const Date hireDate ;
};
//Employee.cpp
#include <iostream>
using std::endl;
using std::cout ;
using std::cin;
#include <cstring >
using std::strlen;
using std::strncpy;
#include "Employee.h"
Employee::Employee(const char * const first,const char * const last
, const Date &dateOfBirth,const Date &dateOfHire)
:birthDate(dateOfBirth),hireDate(dateOfHire)
{
int length = strlen(last);
length = (length < 25 ? length :24);
strncpy(lastName,last ,length);
lastName[length] = '\0';
cout << "Employee object constructor :"
<< firstName << ' ' <<lastName <<endl;
}
void Employee ::print() const
{
cout << lastName << ", " <<firstName << "Hired :";
hireDate.print();
cout << " BirthDay :";
birthDate.print();
cout << endl;
}
Employee::~Employee()
{
cout << "Employee object destructor :"
<< lastName << ", " <<firstName << endl;
}
//main.cpp
#include <iostream>
using std::endl;
using std::cout;
#include "Employee.h"
int main()
{
Date birth(7,24,1949);
Date hire (3,12,1988);
Employee manager("Bob", "Blue",birth,hire);
cout << endl;
manager.print();
cout << "\n Test Date constructor with invalid values:\n";
Date lastDayOff(14,35,1994);
cout <<endl;
return 0;
}
一个简单的小程序,但是为什么在编译的时候会出现multiple definition of Date::Date(……),……,在Date.cpp中,这是什么原因啊?