我想用模板交换两个数字,但为什么这个交换(x,y); 作为一个模棱两可的电话给出错误.
#includeusing namespace std; template 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; }
Ed S... 6
#includeusing namespace std;
iostream
必须包括,algorithm
并且,因为您决定std
在文件中包含整个命名空间,所以您与之发生冲突std::swap
.去掉using namespace std;
编辑:正如@chris在评论中指出的那样,std::swap
被转移到
C++ 11中.