2

Edit: Thank you for your answers! Like I said, this is code provided for a class, so the namespace and name of the function were included. I'm glad to understand what exactly namespace std entails though, and I've included your inputs as comments within my answer (though my answer remains unchanged).

The code below includes the piece I created. This is for a class, so this is the only part that I need judged. My compiler isn't running it (ambiguous calls to overloaded function) but I feel like this is correct.

template <class data_type>
void swap(data_type &a, data_type &b) //swaps 2 variables
{
   data_type c; //temp variable
   c = a;
   a = b;
   b = c;
}

Here is the full code:

#include <iostream>
#include <string>

using namespace std;

template <class data_type>
void swap(data_type &a, data_type &b) //swaps variables
{
   data_type c;
   c = a;
   a = b;
   b = c;
}

int main( )
{
    string x = "first", y = "second";
    int m = 10, n = 20;
    char q = 'Q', r = 'R';

    cout<<"x before swap called = "<<x<<" and y before swap called = "
<<y<<endl;
    swap(x,y);
    cout<<"x after swap called = "<<x<<" and y after swap called = "
<<y<<endl<<endl;

    cout<<"m before swap called = "<<m<<" and n before swap called = "
<<n<<endl;
    swap(m,n);
    cout<<"m after swap called = "<<m<<" and n after swap called = "
<<n<<endl<<endl;

    cout<<"q before swap called = "<<q<<" and r before swap called = "  
<<r<<endl;
    swap(q,r);
    cout<<"q after swap called = "<<q<<" and r after swap called = "   
<<r<<endl<<endl;

    return 0;
}
user135536
  • 23
  • 3
  • 1
    This is why you do not use `using namespace std;`. http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – NathanOliver Apr 05 '17 at 20:05
  • 1
    To be blatantly obvious, the library function `std::swap` is conflicting with your `swap` because you included all the symbols from `std`. – Thomas Matthews Apr 05 '17 at 20:11

3 Answers3

6

The standard library comes with a template std::swap (see cpp reference), and you are defining your own swap-template function. So far no problem, but once you declare using namespace std, any use of swap is ambiguous, since the compiler cannot decide whether to take std::swap or your own swap.

So, as mentioned already by others, avoid using namespace std statement.

Additionally, you could think of declaring "your" swap in an own namespace, like:

namespace class1_exercise {

    template <class data_type>
    void swap(data_type &a, data_type &b) //swaps variables
    {
        data_type c;
        c = a;
        a = b;
        b = c;
    }
}

Thereby, you can make the distinction between "your" swap and the std::swap even more explicit:

std::swap(x,y);
// versus:
class1_exercise::swap(x,y);
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
4

It's you.

//using namespace std;
using std::string;
using std::cout;
using std::endl;
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
  • So my code is fine? Like I said, that part is included by the instructor and is not to be altered. The question is basically how would you turn overloaded functions given into a template function, and I've done that correctly, yes? – user135536 Apr 05 '17 at 20:06
  • 2
    @user135536 show your instructor this answer. `using namespace std;` is a *really bad idea* in real code. – Richard Hodges Apr 05 '17 at 20:09
2

This is the cause of the error

using namespace std;

It's a really really really bad practice to declare "using namespace std" Using the namespace will pull things in that you don't want

There are two alternate solutions

ONE

using std::cout; 
cout << "Values:";

TWO (i'll suggest this one)

std::cout << "Values:";

Note : you could change the name of your swap function but it's always better to adapt a better way so you encounter fewer problems in future

RazaUsman_k
  • 694
  • 6
  • 20