Error codes when compiling, using template function

This is a discussion on Error codes when compiling, using template function within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm pretty new on C++ so maybe this is obvious to you. I'm trying to compile this code with ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    4

    Question Error codes when compiling, using template function

    Hi,

    I'm pretty new on C++ so maybe this is obvious to you.

    I'm trying to compile this code with MSVC. This code is taken from a C++ book as an example using templates.

    Code:
    #include <iostream>
    using namespace std;
    template <typename Any>
    void	swap(Any &a, Any &b);
    
    int main()
    {
    	cout.precision(2);
    	cout.setf(ios::fixed, ios::floatfield);
    	int		i = 10, j = 20;
    	cout << "i, j = " << i << ", " << j << ".\n";
    	swap(i, j);
    	cout << "i, j = " << i << ", " << j << ".\n";
    	return 0;
    }
    
    template <typename Any>
    void	swap(Any &a, Any &b)
    {
    	Any		temp;
    
    	temp = a;
    	a = b;
    	b = temp;
    }
    As I understand, the compiler is supposed to understand which code to produce from the type you send as arguments in the function call. In this case two int's.

    But I get two fault codes:
    error C2667: 'swap' : none of 2 overload have a best conversion
    error C2668: 'swap' : ambiguous call to overloaded function


    Please, someone who can explain what this means?

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    Change the name swap -> my_swap for example.
    swap is already a template function in std namespace.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    4
    You're right. Now it works. And now I understand why it said that it was overloaded.

    Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21