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

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么編譯一直通不過呢?


#include <iostream>

using namespace std;


/**

?* 定義模板函數swapNum

?* 實現功能:交換兩個數的位置

?*/

template <typename T>

void swap(T &a,T &b)


{

? ? T temp = a;

? ? a = b;

? ? b = temp;

}


int main(void)

{

? ? int x = 10;

? ? int y = 20;

? ? // 調用模板函數

? ? swap<int>(x,y);

? ? ? ? cout << "x = " << x << endl;

? ? ? ? cout << "y = " << y << endl;

return 0;

}



l報錯:/477/9670/FmcL/index.cpp: In function 'int main()':
/477/9670/FmcL/index.cpp:22:18: error: call of overloaded 'swap(int&, int&)' is ambiguous
? ? swap(x,y);
? ? ? ? ? ? ? ? ?^
/477/9670/FmcL/index.cpp:22:18: note: candidates are:
/477/9670/FmcL/index.cpp:9:6: note: void swap(T&, T&) [with T = int]
void swap(T &a,T &b)
? ? ?^
In file included from /usr/include/c++/4.8.2/bits/stl_pair.h:59:0,
? ? ? ? ? ? ? ? from /usr/include/c++/4.8.2/bits/stl_algobase.h:64,
? ? ? ? ? ? ? ? from /usr/include/c++/4.8.2/bits/char_traits.h:39,
? ? ? ? ? ? ? ? from /usr/include/c++/4.8.2/ios:40,
? ? ? ? ? ? ? ? from /usr/include/c++/4.8.2/ostream:38,
? ? ? ? ? ? ? ? from /usr/include/c++/4.8.2/iostream:39,
? ? ? ? ? ? ? ? from /477/9670/FmcL/index.cpp:1:
/usr/include/c++/4.8.2/bits/move.h:166:5: note: void std::swap(_Tp&, _Tp&) [with _Tp = int]
? ? swap(_Tp& __a, _Tp& __b)
? ? ^

正在回答

1 回答

需將swap改為swapNum或者其他不同于swap的函數名,因為swap是C++本身就含有的內置函數。

4 回復 有任何疑惑可以回復我~
#1

慕斯卡1160282 提問者

非常感謝!
2016-11-25 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
C++遠征之模板篇
  • 參與學習       91140    人
  • 解答問題       325    個

本C++教程力求即學即會,所有知識以實踐方式講解到操作層面

進入課程

為什么編譯一直通不過呢?

我要回答 關注問題
微信客服

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

幫助反饋 APP下載

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

公眾號

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