2 回答

TA貢獻1836條經(jīng)驗 獲得超3個贊
如果要求是
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++標準的,但是目前的編譯器基本不支持,據(jù)說有一個商業(yè)編譯器支持。
可以參考下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);
};
//其友元函數(shù)函數(shù)也涉及模板
/*
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;
}

TA貢獻1921條經(jīng)驗 獲得超9個贊
如果要求是
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++標準的,但是目前的編譯器基本不支持,據(jù)說有一個商業(yè)編譯器支持。
可以參考下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);
};
//其友元函數(shù)函數(shù)也涉及模板
/*
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;
}
- 2 回答
- 0 關(guān)注
- 176 瀏覽
添加回答
舉報