例如:
类模板
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那些代码的,我这个提问不是水题,自己测试一下就知道不行了
C++中,如何将类模板和函数模板的分文件实现
答案:6 悬赏:50 手机版
解决时间 2021-01-28 07:08
- 提问者网友:十年饮冰
- 2021-01-27 15:10
最佳答案
- 五星知识达人网友:鸠书
- 2021-01-27 15:59
如果要求是
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;
}
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;
}
全部回答
- 1楼网友:何以畏孤独
- 2021-01-27 20:02
写在头文件里,然后在别的cpp里include进来不行吗?
- 2楼网友:摆渡翁
- 2021-01-27 19:39
如果要求是
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;
}
- 3楼网友:等灯
- 2021-01-27 18:49
如果要求是
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;
}
- 4楼网友:等灯
- 2021-01-27 18:08
头文件名称为 DString.h 文件刚开始加上
#ifndef DSTRING_H
#define DSTRING_H
// 类模板声明
末尾加上
#endif
其它头文件类似
- 5楼网友:迷人又混蛋
- 2021-01-27 17:12
如果要求是
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;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯