第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

現(xiàn)在希望使得這段代碼在其他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);};//其友元函數(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]);}}template<int maxLength>ifstream& operator >>(ifstream input,const DString<maxLength>& str1){for(int i=0;i<maxLength;i++){input.get();}}直接把這整段代碼寫進頭文件有問題,當(dāng)多個CPP文件同時引用這個頭文件的時候,在LINK的時候會出現(xiàn)函數(shù)的重復(fù)定義,提示出錯。 
查看完整描述

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;


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

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;
}

 


查看完整回答
反對 回復(fù) 2023-04-23
  • 2 回答
  • 0 關(guān)注
  • 176 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號