473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help in template

72
Hi,

Please see below a peice of code
Expand|Select|Wrap|Line Numbers
  1. template<class T> void swap(T &a, T &b)
  2. {
  3.         T temp;
  4.         temp = a;
  5.         a = b;
  6.         b = temp;
  7. }
  8.  
Here I am getting an error as below
template.cpp:26: error: call of overloaded swap(int&, int&)â is ambiguous

I dont know what is the problem with swap?

If i define template as as template<class T> void swapa(T &a, T &b) It will work fine. Please help to resolve this.

Thanks
Manjunath
Oct 8 '09 #1
6 2124
Banfa
9,065 Expert Mod 8TB
Is there a function with the prototype swap(int&, int&) defined elsewhere in your code?

For instance have you included <algorithm>?
Oct 8 '09 #2
manjuks
72
I didn't include algorithm. I didn't define swap(int&, int&) too. If I define this one, it should work properly without any error as for as my knowledge.
Correct me if I am wrong.

Thanks,
Manjunath
Oct 8 '09 #3
Banfa
9,065 Expert Mod 8TB
Well the compiler error means that you have attempted to call swap in a context where it has more than one option of which function to call and no way to resolve which one it should use perhaps you could post the code that is creating the error.
Oct 8 '09 #4
manjuks
72
Please go through the code and help me out in getting out of that error

#include<iostream>
using namespace std;

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


int main()
{
int a = 100, b = 200;
cout<<"Before swapping"<<endl;
cout<<"a = "<<a<<" "<<"b = "<<b<<endl;
swap(a, b);
cout<<"After swapping"<<endl;
cout<<"a = "<<a<<" "<<"b = "<<b<<endl;
return 0;
}

Thanks,
Manjunath
Oct 8 '09 #5
newb16
687 512MB
main.cpp:18: error: call of overloaded `swap(int&, int&)' is ambiguous
main.cpp:5: error: candidates are: void swap(T&, T&) [with T = int]
/usr/include/c++/3.3.3/bits/stl_algobase.h:121: error: void
>std::swap(_Tp&, _Tp&) [with _Tp = int]

Because iostream included algorithm.
Either dump 'using namespace std' from your program, place your template in some other namespace or rename it.
Oct 8 '09 #6
manjuks
72
Ok Thanks for your help Banfa and newb16,

Now I understood whats the problem.

Thanks Once again,
--Manjunath
Oct 8 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Bernard | last post by:
Hi, I have a problem with a CGI script (Perl) on a Win2000 server. The script is for sending E-cards and was written by Jason Maloney:...
4
by: Rex_chaos | last post by:
Hi all, As some book tells, I try the following example of expression template. template < typename LeftOpd, typename Op, typename RightOpd > struct LOP { LeftOpd lod; RightOpd rod;
0
by: johkar | last post by:
My XML and XSL is below. Also below is a textual representation of what I want to get out of the XML with XSL. For each Extension node in XML, I am only concerned with those nodes with...
7
by: Christian Christmann | last post by:
Hi, in the past I always appreciated your help and hope that you also can help me this time. I've spent many many hours but still can't solve the problem by myself and you are my last hope. ...
1
by: sommarlov | last post by:
Hi everyone >From one of our systems an xml file is produced. I need to validate this file before we send it to an external system for a very lenghty process. I cannot change the xml file layout....
2
by: shapper | last post by:
Hello, I am for days trying to apply a XSL transformation to a XML file and display the result in a the browser. I am using Asp.Net 2.0. Please, could someone post just a simple code example,...
4
by: Debbiedo | last post by:
My software program outputs an XML Driving Directions file that I need to input into an Access table (although if need be I can import a dbf or xls) so that I can relate one of the fields...
8
by: slizorn | last post by:
Hi guys, I have this coding that I wanted to try out. Basically this is meant to be done in Java as practice for the Topic trees in data structures and algorithms. I have recently learned C++ on...
2
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. basically i need to read in a text file... shown below H H,E,L E,B,F B,A,C A,null,null c,null,D
2
by: slizorn | last post by:
hi guys, another problem i am facing with this program.. i have created a method to read in values from a file and store them into TreeNodes of a Tree please help me to solve the problem below.....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.