现在希望使得这段代码在其他CPP文件里仍可生效,如何做到?

例如:
类模板
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文件同时引用这个头文件的时候,在LINK的时候会出现函数的重复定义,提示出错。 

慕哥6287543
浏览 107回答 2
2回答

米脂

如果要求是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;}&nbsp;

郎朗坤

如果要求是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;}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP