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

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

C語言函數模板問題?

C語言函數模板問題?

慕姐4208626 2019-02-06 16:06:35
#include "stdafx.h"#include <iostream>template <class T>void swap(T &a,T &b);int main(){using namespace std;int i=20;int j=30;cout<<"i,j="<<i<<j<<endl;swap(i,j);cout<<i<<","<<j<<endl;getchar();return 0;}void swap(T &a,T &b){int temp;temp=a;a=b;b=temp;}為什么報錯呢?
查看完整描述

3 回答

?
富國滬深

TA貢獻1790條經驗 獲得超9個贊

#include <iostream>
using namespace std; //引入std命名空間
template <class T>
void myswap(T &a,T &b); //swap在iostream定義過了,換個名字
int main(){
using namespace std;
int i=20;
int j=30;
cout<<"i,j="<<i<<j<<endl;
myswap(i,j);
cout<<i<<","<<j<<endl;
getchar();
return 0;
}
template<class T> //這個還是要寫一遍
void myswap(T &a,T &b){
T temp; //temp是T類型
temp=a;
a=b;
b=temp;
}



查看完整回答
反對 回復 2019-03-22
?
ABOUTYOU

TA貢獻1812條經驗 獲得超5個贊

#include "stdafx.h"
#include <iostream>
template <class T>
void myswap(T &a,T &b); //因為標準模板庫中有swap函數,系統不能識別,我只要函數名改一下就可以了
int main(){
using namespace std;
int i=20;
int j=30;
cout<<"i,j="<<i<<j<<endl;
swap(i,j);
cout<<i<<","<<j<<endl;
getchar();
return 0;
}
template <class T> //這里也需要
void myswap(T &a,T &b){
T temp;
temp=a;
a=b;
b=temp;
}



查看完整回答
反對 回復 2019-03-22
  • 3 回答
  • 0 關注
  • 1068 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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