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

  • SEARCH
  • Home
  • 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 8493489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:01:31+00:00 2026-06-10T23:01:31+00:00

How can I modify the below code so that I could use the same

  • 0

How can I modify the below code so that I could use the same template T even in call() function?

#include<iostream>

using namespace std;

template<class T>
void swap(T &x,T &y)
{
 T temp=x;
 x=y;
 y=temp;
}

/*if i use again the"template<class T>" here i have the error "Identifier T is 
undefined" to be gone but then if i compile i have several other errors.. like  
"could be 'void swap<T>(T &,T &)' " and some other errors*/

void call(T &x,T &y)
{
 swap(x,y);
 cout<<x<<y;
}

int main()
{
 int num;
 cout<<"Enter 1 or 0 for int or float\n";
 cin>>num;
 if(num)
 {
  int a,b;
  cin>>a>>b;
  call(a,b);
 }
 else 
 {
  float a,b;
  cin>>a>>b;
  call(a,b);
 }
}

A template function is associated with the template declaration at the start. Can’t the same template be used again for another function? Is there an other way to modify the above code so that I could use the same or any other template in call() function? Overall I need to manage all functions using templates itself.

  • 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-10T23:01:33+00:00Added an answer on June 10, 2026 at 11:01 pm
    template <typename T>
    void call(T &x, T &y)
    {
       swap(x, y);
       cout << x<< y;
    }
    

    should work.

    The problem is probably because you have using namespace std; and there already exists std::swap, so it’s ambiguous.

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

Sidebar

Related Questions

I can't figure out how to modify the below code to include a toggle
I am using the code below, its basically babylonian method, how can i modify
Is there anyway I can modify this code example #include <stdlib.h> #include <iostream> class
Can someone tell me how to modify the code below so that it filters
I'm hoping someone can modify my code below to show me exactly how to
I have a method (shown below) that I discovered can be reused in code
please look at the simple php webpage code below how can modify my code
Question summary: How do I modify the code below so that untrusted, dynamically-loaded code
I am using below function to submit score to game center. How to modify
In android the Toggle buttons are looks like below - Can we modify this

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.