Having trouble with a template

Gosh, I'm about to sound like a total idiot.

Why isn't this code working?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

template <typename T>
void swap(T& a,T& b)
{
    T temp = a;
    a = b;
    b = temp;
}

int main()
{
    string a = "World!";
    string b = "Hello, ";
    swap<string> (a,b);
    cout << a << b << endl;
}


My IDE says "call to swap is ambiguous."
Last edited on
Correct me if I'm wrong, but shouldn't it be 'World!' and 'Hello, '? I think you need to use 's, not "s.
Take out the using namespace std
Oh! Name conflict! Thanks!
Did your compiler not tell you something like this (i.e where it found conflicting
function names)??


D:\Programming\projects\__Codeblocks__\test1\main.cpp||In function 'int main()':|
D:\Programming\projects\__Codeblocks__\test1\main.cpp|196|error: call of overloaded 'swap(int&, int&)' is ambiguous|
D:\Programming\projects\__Codeblocks__\test1\main.cpp|196|note: candidates are:|
D:\Programming\projects\__Codeblocks__\test1\main.cpp|187|note: void swap(T&, T&) [with T = int]|
c:\mingw\bin\..\lib\gcc\mingw32\4.6.1\include\c++\bits\move.h|122|note: void std::swap(_Tp&, _Tp&) [with _Tp = int]|
||=== Build finished: 4 errors, 0 warnings ===|

Ahaanomegas, I'm correcting you because you're wrong. "s are used for strings, 's are used only for a single char.

1
2
string myName = "PaulthePenguin";
char firstInitial = 'p';
Topic archived. No new replies allowed.