求助,请大家看看我那里写错了,照书上写的,只是把Date类改成模板了

猴头 2010-12-01 11:55:28
开始我把代码贴了一部分,不完整,现在修改了其中的一些错误重新发一遍,代码是在vs2008上编译的

#ifndef DATE_H
#define DATE_H

#include<iostream>
using namespace std;

template<class T>
class Date
{
public:
Date(T = 1,T= 1,T=1900);
void print() const;
~Date();
private:
T month;
T day;
T year;
T checkDate(T) const;
};
#endif

#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include"Date.h"
template<class T>
class Employee
{
public:
Employee(const char* const, const char* const, const Date<T>& , const Date<T>& );
void print() const;
~Employee()
{
cout<<"Employee object destructor: "<<lastName<<", "<<firstName<<endl;
}
private:
char firstName[25];
char lastName[25];
const Date<T> birthDay;
const Date<T> hireDate;
};

#endif

#include"Date.h"

template<class T>
Date<T>::Date(T mn, T dy, T yr)
{
if(mn>0&&mn<=12)
month=mn;
else
{
month=1;
cout<<"InValid month("<<mn<<")set to 1"<<endl;
}
year=yr;
day=checkDate(dy);
cout<<"date object constructor for date"<<endl;
print();
cout<<endl;
}

template<class T>
void Date<T>::print() const
{
cout<<month<<'-'<<day<<'-'<<'year'<<endl;
}

template<class T>
Date<T>::~Date()
{
cout<<"Date 的析构函数正在运行......"<<endl;
print();
cout<<"Date 的析构函数运行结束..end.."<<endl;
}

template<class T>
T Date<T>::checkDate(T testday) const
{
static const T daypermonth[13]=
{0,31,28,31,30,31,30,31,31,30,31,30,31};

if(testday>0&&testday<=daypermonth[month])
return testday;

if((2==month)&&(29==testday)&&((0==year%400)||(0==year%4)&&(0!=year%100)))
return testday;

cout<<"Invalid day ("<<testday<<")set to 1"<<endl;
return 1;
}

#include"Employee.h"

#include<string>
//using std::strlen;
//using std::strncpy;

#include<iostream>
using namespace std;

#include"Date.h"

template<class T>
Employee<T>::Employee(const char* const first, const char* const last, const Date<T>& dateOfBirth, const Date<T>& dateOfHire)
:birthDay(dateOfBirth),
hireDate(dateOfHire)
{
int length=strlen(first);
length = (length<25?length:24);
strncpy(firstName,first,length);
firstName[length]='\0';

length = strlen(last);
length = (length<25?length:24);
strncpy(lastname,last,length);
firstName[length]='\0';

cout<<"Employee object constructor:"<<firstName<<' '<<lastname<<endl;
}

template<class T>
void Employee<T>::print() const
{
cout<<lastName<<", "<<firstName<<" Hired: ";
hireDate.print();
cout<<" Birthday: ";
birthDay.print();
cout<<endl<<endl;
}

/*template<class T>
Employee<T>::~Employee()
{
cout<<"Employee object destructor: "<<lastName<<", "<<firstName<<endl;
}*/

#include<iostream>
using namespace std;

#include"Employee.h"

int main()
{
Date<int>birth(7,24,1949);
Date<int>hire(3,12,1988);
Employee<int> manager("bob","blue",birth,hire);

cout<<endl<<endl;
manager.print();

cout<<"\nTest Date constructor with invalid value : "<<endl;
Date<int>lastDayOff(14,35,1994);
cout<<endl;

system("pause");
return 0;
}
...全文
113 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
AlwaysSLH 2010-12-02
  • 打赏
  • 举报
回复
模板类的实现放到头文件里,不要放在cpp文件里
猴头 2010-12-02
  • 打赏
  • 举报
回复
谢谢 搞定了
[Quote=引用 9 楼 hai040 的回复:]
引用 7 楼 yan_hyz 的回复:
我把代码全放到同一个CPP文件下还是出现问题这次出现的问题是:
c:\documents and settings\hyz\桌面\class_include_object_in_only_file\date.h(39) : error C2995: 'Date<T>::Date<T>' : template function has already be……
[/Quote]
zhengjiankang 2010-12-02
  • 打赏
  • 举报
回复
模板类的实现要全部写在头文件里
需要在编译期确定类型

Date类也用模板来写
不知道有什么意义 求楼下解释
hai040 2010-12-02
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yan_hyz 的回复:]
我把代码全放到同一个CPP文件下还是出现问题这次出现的问题是:
c:\documents and settings\hyz\桌面\class_include_object_in_only_file\date.h(39) : error C2995: 'Date<T>::Date<T>' : template function has already been defined
c:\docu……
[/Quote]
把#include "date.h"去掉
bdmh 2010-12-02
  • 打赏
  • 举报
回复
好像是方法被重复定义了
猴头 2010-12-02
  • 打赏
  • 举报
回复
我把代码全放到同一个CPP文件下还是出现问题这次出现的问题是:
c:\documents and settings\hyz\桌面\class_include_object_in_only_file\date.h(39) : error C2995: 'Date<T>::Date<T>' : template function has already been defined
c:\documents and settings\hyz\桌面\class_include_object_in_only_file\date.h(11) : see declaration of 'Date<T>::Date<T>'
c:\documents and settings\hyz\桌面\class_include_object_in_only_file\date.h(45) : error C2995: 'print' : template function has already been defined
c:\documents and settings\hyz\桌面\class_include_object_in_only_file\date.h(12) : see declaration of 'print'
c:\documents and settings\hyz\桌面\class_include_object_in_only_file\date.h(53) : error C2995: 'Date<T>::~Date<T>' : template function has already been defined
c:\documents and settings\hyz\桌面\class_include_object_in_only_file\date.h(13) : see declaration of 'Date<T>::~Date<T>'
c:\documents and settings\hyz\桌面\class_include_object_in_only_file\date.h(69) : error C2995: 'checkDate' : template function has already been defined
c:\documents and settings\hyz\桌面\class_include_object_in_only_file\date.h(18) : see declaration of 'checkDate'
Error executing cl.exe.

[Quote=引用 3 楼 mstlq 的回复:]
http://www.cppblog.com/michaelgao/archive/2008/10/09/63571.html
[/Quote]
luciferisnotsatan 2010-12-02
  • 打赏
  • 举报
回复
模板类的实现是放在头文件.h里的,不能放源文件.cpp里
猴头 2010-12-02
  • 打赏
  • 举报
回复
我把文件放在一起试试
游牧小小诗人 2010-12-02
  • 打赏
  • 举报
回复
学习下,,还没用过模版呢。。。
猴头 2010-12-01
  • 打赏
  • 举报
回复
编译器提示的错误如下:
>main.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Date<int>::~Date<int>(void)" (??1?$Date@H@@QAE@XZ),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall Employee<int>::print(void)const " (?print@?$Employee@H@@QBEXXZ),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Employee<int>::Employee<int>(char const * const,char const * const,class Date<int> const &,class Date<int> const &)" (??0?$Employee@H@@QAE@QBD0ABV?$Date@H@@1@Z),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Date<int>::Date<int>(int,int,int)" (??0?$Date@H@@QAE@HHH@Z),该符号在函数 _main 中被引用
1>.\Debug/Class_Include_Object.exe : fatal error LNK1120: 4 个无法解析的外部命令
1>生成日志保存在“file://i:\学习测试\Class_Include_Object\Debug\BuildLog.htm”
1>Class_Include_Object - 5 个错误,0 个警告

65,208

社区成员

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

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