user3078874
user3078874

Reputation: 1

Why can I instantiate my C++ template?

Not sure what the problem is..

$ g++ test2.C test2.C: In function 'int main()': test2.C:25: error: call of overloaded 'swap(int&, int&)' is ambiguous test2.C:8: note: candidates are: 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;
}

Upvotes: 0

Views: 70

Answers (1)

Glenn Teitelbaum
Glenn Teitelbaum

Reputation: 10343

Try naming your swap myswap (or anything else) or putting it in a namespace

because you have using namespace std; it is confused between your swap and std::swap

In general always avoid using namespace std; Definately in headers but as here, it can cause problems in .cpp files as well

Upvotes: 3

Related Questions