为什么会出现下面的问题?

HicHerine 2012-02-16 08:19:15
//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中,这是什么原因啊?
...全文
119 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
HicHerine 2012-04-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

#include "Date.cpp"

这种写法还是第一次看见呢,包含一般都是包含.h文件,估计问题就出在这里了
[/Quote]
在建立工程的时候没有将头文件引进去,所以在引用文件的时候是.cpp
漫步者、 2012-02-18
  • 打赏
  • 举报
回复

#include "Date.cpp"//这个你引用错了吧,应该是.h才对吧
HicHerine 2012-02-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cxsjabcabc 的回复:]

//Employee.h

#include "Date.cpp"

class Employee

这个地方为什么包含的是cpp文件...
[/Quote]我在CODEBLOCKS上编译时如果是#include "Date.h"的话很多时候好像编译不过去,而且会提示Date类中的任何方法或者成员变量都会是undefined,但是改为"Date.cpp"时这些都可以通过
陈思有 2012-02-18
  • 打赏
  • 举报
回复
可以啊,但是我发现输出时有点问题,因为你在employee类里面只有对lastname进行处理,而没有对firstname进行处理
xspace_time 2012-02-18
  • 打赏
  • 举报
回复
你把Date(int = 1,int = 1,int = 1900);改成
Date::Date(int mn=1, int dy=1, int yr=1900);试下
Jim_King_2000 2012-02-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 zhang525jia 的回复:]

引用 3 楼 cxsjabcabc 的回复:

//Employee.h

#include "Date.cpp"

class Employee

这个地方为什么包含的是cpp文件...
我在CODEBLOCKS上编译时如果是#include "Date.h"的话很多时候好像编译不过去,而且会提示Date类中的任何方法或者成员变量都会是undefined,但是改为"Date.……
[/Quote]
不可能的。
Jim_King_2000 2012-02-17
  • 打赏
  • 举报
回复
//Employee.h

#include "Date.cpp"

是不是应该包含头文件啊?
Fungyun 2012-02-17
  • 打赏
  • 举报
回复
我把上述代码放在一个cpp里面编译,是没有问题的。
qixing1115 2012-02-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xiaohuh421 的回复:]
#include "Date.cpp"

这种写法还是第一次看见呢,包含一般都是包含.h文件,估计问题就出在这里了
[/Quote]
Eleven 2012-02-17
  • 打赏
  • 举报
回复
#include "Date.cpp"
是不是写错了#include "Date.h" ??
xiaohuh421 2012-02-17
  • 打赏
  • 举报
回复
#include "Date.cpp"

这种写法还是第一次看见呢,包含一般都是包含.h文件,估计问题就出在这里了
程序员小迷 2012-02-17
  • 打赏
  • 举报
回复
//Employee.h

#include "Date.cpp"

class Employee

这个地方为什么包含的是cpp文件...

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧