我想使用模板交换两个数字,但为什么会这样 swap(x, y);给出一个错误作为不明确的调用。
#include <iostream>
using namespace std;
template <class T>
void swap(T &a, T &b) {
T temp = a;
a = b;
b = temp;
}
int main () {
int x = 14;
int y = 7;
swap(x, y);
cout << x << y;
}
#include <iostream>
using namespace std;
iostream
必须包括algorithm
并且,既然您决定包括整个std
文件中的名称空间,您与std::swap
。消除using namespace std;
编辑:正如@chris 在评论中指出的那样,std::swap
被移至<utility>
在 C++11 中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)