如果要求是
template
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
ofstream& operator <<(ofstream output,const DString
for(int i=0;i
}
}
template
ifstream& operator >>(ifstream input,const DString
for(int i=0;i
}
}
放在一个cpp文件中,这种写法是符合C++标准的,但是目前的编译器基本不支持,据说有一个商业编译器支持。
可以参考下boost,一般模板类都是全部写在一个.h文件中。
另外上面的程序有好几个警告。
以下修改过,用g++编译通过。
#include
#include
using namespace std;
template
class DString{
public:
char text[maxLength];
public:
static const int size=maxLength;
friend ofstream& operator <<(ofstream output,const DString
for(int i=0;i
}
return output;
}
friend ifstream& operator >> <>(ofstream input,const DString& str1);
};
//其友元函数函数也涉及模板
/*
template
ofstream& operator <<(ofstream output,const DString
for(int i=0;i
}
return output;
}*/
template
ifstream& operator >>(ifstream input,const DString
for(int i=0;i
}
return input;
}
int main(int argc, char *argv[])
{
return 0;
}
如果要求是
template
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
ofstream& operator <<(ofstream output,const DString
for(int i=0;i
}
}
template
ifstream& operator >>(ifstream input,const DString
for(int i=0;i
}
}
放在一个cpp文件中,这种写法是符合C++标准的,但是目前的编译器基本不支持,据说有一个商业编译器支持。
可以参考下boost,一般模板类都是全部写在一个.h文件中。
另外上面的程序有好几个警告。
以下修改过,用g++编译通过。
#include
#include
using namespace std;
template
class DString{
public:
char text[maxLength];
public:
static const int size=maxLength;
friend ofstream& operator <<(ofstream output,const DString
for(int i=0;i
}
return output;
}
friend ifstream& operator >> <>(ofstream input,const DString& str1);
};
//其友元函数函数也涉及模板
/*
template
ofstream& operator <<(ofstream output,const DString
for(int i=0;i
}
return output;
}*/
template
ifstream& operator >>(ifstream input,const DString
for(int i=0;i
}
return input;
}
int main(int argc, char *argv[])
{
return 0;
}
头文件名称为 DString.h 文件刚开始加上
#ifndef DSTRING_H
#define DSTRING_H
// 类模板声明
末尾加上
#endif
其它头文件类似
export