永发信息网

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

答案:6  悬赏:50  手机版
解决时间 2021-01-28 07:08
例如:
类模板
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那些代码的,我这个提问不是水题,自己测试一下就知道不行了
最佳答案
如果要求是
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>
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;
}
全部回答
写在头文件里,然后在别的cpp里include进来不行吗?
如果要求是 template&lt;int maxlength&gt; class dstring{ public: char text[maxlength]; public: static const int size=maxlength; friend ofstream&amp; operator &lt;&lt;(ofstream output,const dstring&amp; str1); friend ifstream&amp; operator &gt;&gt;(ofstream input,const dstring&amp; str1); }; 放在一个.h文件中,而 template&lt;int maxlength&gt; ofstream&amp; operator &lt;&lt;(ofstream output,const dstring&lt;maxlength&gt;&amp; str1){ for(int i=0;i&lt;maxlength;i++){ output.put(str1.text[i]); } } template&lt;int maxlength&gt; ifstream&amp; operator &gt;&gt;(ifstream input,const dstring&lt;maxlength&gt;&amp; str1){ for(int i=0;i&lt;maxlength;i++){ input.get(); } } 放在一个cpp文件中,这种写法是符合c++标准的,但是目前的编译器基本不支持,据说有一个商业编译器支持。 可以参考下boost,一般模板类都是全部写在一个.h文件中。 另外上面的程序有好几个警告。 以下修改过,用g++编译通过。 #include &lt;iostream&gt; #include &lt;fstream&gt; using namespace std; template&lt;int maxlength&gt; class dstring{ public: char text[maxlength]; public: static const int size=maxlength; friend ofstream&amp; operator &lt;&lt;(ofstream output,const dstring&lt;maxlength&gt;&amp; str1){ for(int i=0;i&lt;maxlength;i++){ output.put(str1.text[i]); } return output; } friend ifstream&amp; operator &gt;&gt; &lt;&gt;(ofstream input,const dstring&amp; str1); }; //其友元函数函数也涉及模板 template&lt;int maxlength&gt; ifstream&amp; operator &gt;&gt;(ifstream input,const dstring&lt;maxlength&gt;&amp; str1){ for(int i=0;i&lt;maxlength;i++){ input.get(); } return input; } int main(int argc, char *argv[]) { return 0; }
如果要求是 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> 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; }
头文件名称为 DString.h 文件刚开始加上 #ifndef DSTRING_H #define DSTRING_H // 类模板声明 末尾加上 #endif 其它头文件类似
如果要求是 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> 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; }
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
诺蒂斯卡钢琴价格现在什么价格
【初二物理题】初二物理题.
是以结构图,还是以建施图纸算
相邻两个计数单位之间的进率是10,被称为什么
贱获的意思是什么啊?知道的请说下!
成语一个沙字,一座塔
美拍被封号怎么解封?
我中午睡觉时做了一个梦,环境是这样的,那时
魔兽世界巨魔德鲁伊10级做任务突然就变成枭兽
科学的意思是什么啊?知道的请说下!
下列资产负债表各项目不能以总账余额直接填列
以小博大的颜语是什么
怎么知道一个网站域名是什么啊! 5分
佛手麦的意思是什么啊?知道的请说下!
2008年北京奥运会期间,中国基督教会派遣了16
推荐资讯
请问喝苦丁茶会影响生育吗?
php怎么新建多级目录
国际漫游时能够使用呼叫转移功能?
一般情况下,借款人自有资金=().
术法的意思是什么啊?知道的请说下!
艮岑的意思是什么啊?知道的请说下!
从成都五桂桥车站到五块石车站坐出租车要多少
不要让善良成为愚蠢的挡箭牌 什么意思 我不懂
黥徒的意思是什么啊?知道的请说下!
IPHONE手机QQ的说说的草稿箱在哪里找啊
加索尔以转会到哪去了?
胜境的意思是什么啊?知道的请说下!
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?