为什么我可以实例化我的C ++模板?

时间:2013-12-08 01:10:09

标签: templates gcc

不确定问题是什么..

$ g ++ test2.C test2.C:在函数'int main()'中: test2.C:25:错误:调用重载'swap(int&,int&)'是不明确的 test2.C:8:注意:候选人是:void swap(T&,T&)[with T = int] /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h:92:note:void std: :swap(_Tp&,_Tp&)[with _Tp = int]

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

template <class T>
inline void swap(T& i, T& j) { 
T temp = i; i = j; j = temp;
}




main() {
int m,n;
cout << "Enter integer :";

cin >> m;
cout << "Enter integer :";

cin >> n;
cout << m << "," << n << endl;

swap(m,n);

cout << m << "," << n << endl;
}

1 个答案:

答案 0 :(得分:3)

尝试命名交换myswap(或其他任何内容)或将其放入命名空间

因为你有using namespace std;,你的swap和std :: swap

之间会混淆

一般情况下,总是在标题中明确避免使用using namespace std;,但在此处,它也会导致.cpp文件出现问题