Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8878643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:45:40+00:00 2026-06-14T19:45:40+00:00

I started learning about templates and I copied the code from my book but

  • 0

I started learning about templates and I copied the code from my book but the compiler is giving me this error

Error 1 error: ‘swap’ : ambiguous call to overloaded function”

Here is my program

#include <iostream>

template <typename X>
void swap(X &a, X &b);

int _tmain(){
    using namespace std;
    int a, b;

    cout << "enter two numbers:\n";
    cin >> a >> b;
    cout << "Your numbers are: " << a << ", " << b << endl;

    swap(a, b); //error is here
    cout << "Your numbers reversed are: " << a << ", " << b << endl;

    return 0;
}

template <typename X>
void swap(X &a, X &b){
    X temp = a;
    a = b;
    b = temp;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:45:41+00:00Added an answer on June 14, 2026 at 7:45 pm

    You are redefining the function swap with the same signature, so you’re having an ambiguous definition of it, and, consequently, an ambiguous call.

    If you want to maintain the function with the same signature, you should either choose to use not the “using namespace std;”, which will not shadow your swap() definition, or simply define tthe function in another namespace.

    Example:

    namespace your_namespace {
    
        template <typename X>
        void swap(X &a, X &b) {
            X temp = a;
            a = b;
            b = temp;
        }
    
    }
    
    int foo(10), bar(20);
    std::cout << "foo: " << foo << "; bar: " << bar << std::endl;
    
    your_namespace::swap(foo, bar);
    std::cout << "foo: " << foo << "; bar: " << bar << std::endl;
    

    Output:

    foo: 10; bar: 20
    foo: 20; bar: 10
    

    Regards!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just started learning about thread safety. This is making me code a lot
I just recently have started learning about the wonders of **kwargs but I've hit
I've just started learning about hash tables and I understand how to insert but
I've only started learning Java about 3 months ago and this is my first
I have started learning about Windows 8 Store Apps. I recall from Silverlight and
I recently started learning about ASP.Net MVC and its various features MVC_3_MUSIC_STORE + CODE
Very confused over this. I've just started learning about pointers and have now decided
I recently started learning about developing in android and now I'm getting an error
I've just started learning about parsing, and I wrote this simple parser in Haskell
Recently I've started learning about Contexts in .NET (context-bound, context-agile, message sinks, etc.). Several

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.