C++中,如何将类模板和函数模板的分文件实现

例如:类模板template<intmaxLength>classDString{public:chartext[maxLength];public:staticcons... 例如:
类模板
template<int maxLength>
class DString{
public:
char text[maxLength];
public:
static const int size=maxLength;
friend ofstream& operator <<(ofstream output,const DString& str1);
friend ifstream& operator >>(ofstream input,const DString& str1);
};

//其友元函数函数也涉及模板
template<int maxLength>
ofstream& operator <<(ofstream output,const DString<maxLength>& str1){
for(int i=0;i<maxLength;i++){
output.put(str1.text[i]);
}
}
template<int maxLength>
ifstream& operator >>(ifstream input,const DString<maxLength>& str1){
for(int i=0;i<maxLength;i++){
input.get();
}
}

现在希望使得这段代码在其他CPP文件里仍可生效,如何做到,大神速进,高分悬赏
直接把这整段代码写进头文件有问题,当多个CPP文件同时引用这个头文件的时候,在LINK的时候会出现函数的重复定义,提示出错。

说白了就是想让这段代码能够复用

写什么ifndef那些代码的,我这个提问不是水题,自己测试一下就知道不行了
展开
 我来答
dielsalder
推荐于2016-09-29 · TA获得超过3854个赞
知道大有可为答主
回答量:1864
采纳率:50%
帮助的人:2804万
展开全部
如果要求是
template<int maxLength>
class DString{
public:
char text[maxLength];
public:
static const int size=maxLength;
friend ofstream& operator <<(ofstream output,const DString& str1);
friend ifstream& operator >>(ofstream input,const DString& str1);
};
放在一个.h文件中,而
template<int maxLength>
ofstream& operator <<(ofstream output,const DString<maxLength>& str1){
for(int i=0;i<maxLength;i++){
output.put(str1.text[i]);
}
}
template<int maxLength>
ifstream& operator >>(ifstream input,const DString<maxLength>& str1){
for(int i=0;i<maxLength;i++){
input.get();
}
}
放在一个cpp文件中,这种写法是符合C++标准的,但是目前的编译器基本不支持,据说有一个商业编译器支持。
可以参考下boost,一般模板类都是全部写在一个.h文件中。
另外上面的程序有好几个警告。
以下修改过,用g++编译通过。
#include <iostream>
#include <fstream>
using namespace std;

template<int maxLength>
class DString{
public:
char text[maxLength];
public:
static const int size=maxLength;
friend ofstream& operator <<(ofstream output,const DString<maxLength>& str1){
for(int i=0;i<maxLength;i++){
output.put(str1.text[i]);
}
return output;
}
friend ifstream& operator >> <>(ofstream input,const DString& str1);
};

//其友元函数函数也涉及模板
/*
template<int maxLength>
ofstream& operator <<(ofstream output,const DString<maxLength>& str1){
for(int i=0;i<maxLength;i++){
output.put(str1.text[i]);
}
return output;
}*/
template<int maxLength>
ifstream& operator >>(ifstream input,const DString<maxLength>& str1){
for(int i=0;i<maxLength;i++){
input.get();
}
return input;
}
int main(int argc, char *argv[])
{

return 0;
}
博思aippt
2024-07-20 广告
作为深圳市博思云创科技有限公司的工作人员,对于Word文档生成PPT的操作,我们有以下建议:1. 使用另存为功能:在Word中编辑完文档后,点击文件->另存为,选择PowerPoint演示文稿(*.pptx)格式,即可将文档内容转换为PPT... 点击进入详情页
本回答由博思aippt提供
百度网友9e9b632
推荐于2016-05-06 · TA获得超过847个赞
知道小有建树答主
回答量:247
采纳率:50%
帮助的人:27.5万
展开全部
如果要求是
template<int maxLength>
class DString{
public:
char text[maxLength];
public:
static const int size=maxLength;
friend ofstream& operator <<(ofstream output,const DString& str1);
friend ifstream& operator >>(ofstream input,const DString& str1);
};
放在一个.h文件中,而
template<int maxLength>
ofstream& operator <<(ofstream output,const DString<maxLength>& str1){
for(int i=0;i<maxLength;i++){
output.put(str1.text[i]);
}
}
template<int maxLength>
ifstream& operator >>(ifstream input,const DString<maxLength>& str1){
for(int i=0;i<maxLength;i++){
input.get();
}
}
放在一个cpp文件中,这种写法是符合C++标准的,但是目前的编译器基本不支持,据说有一个商业编译器支持。
可以参考下boost,一般模板类都是全部写在一个.h文件中。
另外上面的程序有好几个警告。
以下修改过,用g++编译通过。
#include <iostream>
#include <fstream>
using namespace std;

template<int maxLength>
class DString{
public:
char text[maxLength];
public:
static const int size=maxLength;
friend ofstream& operator <<(ofstream output,const DString<maxLength>& str1){
for(int i=0;i<maxLength;i++){
output.put(str1.text[i]);
}
return output;
}
friend ifstream& operator >> <>(ofstream input,const DString& str1);
};

//其友元函数函数也涉及模板
/*
template<int maxLength>
ofstream& operator <<(ofstream output,const DString<maxLength>& str1){
for(int i=0;i<maxLength;i++){
output.put(str1.text[i]);
}
return output;
}*/
template<int maxLength>
ifstream& operator >>(ifstream input,const DString<maxLength>& str1){
for(int i=0;i<maxLength;i++){
input.get();
}
return input;
}
int main(int argc, char *argv[])
{

return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
双枪老椰子
2015-06-07 · TA获得超过5.5万个赞
知道大有可为答主
回答量:5797
采纳率:93%
帮助的人:1987万
展开全部
如果要求是
template<int maxLength>
class DString{
public:
char text[maxLength];
public:
static const int size=maxLength;
friend ofstream& operator <<(ofstream output,const DString& str1);
friend ifstream& operator >>(ofstream input,const DString& str1);
};
放在一个.h文件中,而
template<int maxLength>
ofstream& operator <<(ofstream output,const DString<maxLength>& str1){
for(int i=0;i<maxLength;i++){
output.put(str1.text[i]);
}
}
template<int maxLength>
ifstream& operator >>(ifstream input,const DString<maxLength>& str1){
for(int i=0;i<maxLength;i++){
input.get();
}
}
放在一个cpp文件中,这种写法是符合C++标准的,但是目前的编译器基本不支持,据说有一个商业编译器支持。
可以参考下boost,一般模板类都是全部写在一个.h文件中。
另外上面的程序有好几个警告。
以下修改过,用g++编译通过。
#include <iostream>
#include <fstream>
using namespace std;

template<int maxLength>
class DString{
public:
char text[maxLength];
public:
static const int size=maxLength;
friend ofstream& operator <<(ofstream output,const DString<maxLength>& str1){
for(int i=0;i<maxLength;i++){
output.put(str1.text[i]);
}
return output;
}
friend ifstream& operator >> <>(ofstream input,const DString& str1);
};

//其友元函数函数也涉及模板
/*
template<int maxLength>
ofstream& operator <<(ofstream output,const DString<maxLength>& str1){
for(int i=0;i<maxLength;i++){
output.put(str1.text[i]);
}
return output;
}*/
template<int maxLength>
ifstream& operator >>(ifstream input,const DString<maxLength>& str1){
for(int i=0;i<maxLength;i++){
input.get();
}
return input;
}
int main(int argc, char *argv[])
{

return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
泊进√62a8648
2010-08-31 · TA获得超过502个赞
知道小有建树答主
回答量:257
采纳率:0%
帮助的人:297万
展开全部
头文件名称为 DString.h 文件刚开始加上
#ifndef DSTRING_H
#define DSTRING_H
// 类模板声明
末尾加上
#endif

其它头文件类似
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
悲欢归去来3425
2010-08-31 · TA获得超过364个赞
知道小有建树答主
回答量:412
采纳率:0%
帮助的人:333万
展开全部
export
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 2条折叠回答
收起 更多回答(4)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式