亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

現在希望使得這段代碼在其他CPP文件里仍可生效,如何做到?

現在希望使得這段代碼在其他CPP文件里仍可生效,如何做到?

C++
慕哥6287543 2023-04-20 19:15:27
例如:類模板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的時候會出現函數的重復定義,提示出錯。 
查看完整描述

2 回答

?
米脂

TA貢獻1836條經驗 獲得超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++標準的,但是目前的編譯器基本不支持,據說有一個商業編譯器支持。
可以參考下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;


查看完整回答
反對 回復 2023-04-23
?
郎朗坤

TA貢獻1921條經驗 獲得超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++標準的,但是目前的編譯器基本不支持,據說有一個商業編譯器支持。
可以參考下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;
}

 


查看完整回答
反對 回復 2023-04-23
  • 2 回答
  • 0 關注
  • 181 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號