How to avoid template ambiguity

This is a discussion on How to avoid template ambiguity within the C++ Programming forums, part of the General Programming Boards category; How do you avoid template ambiguity when working with templates and the class keyword. consider this example where the templated ...

  1. #1
    Shadow12345
    Guest

    How to avoid template ambiguity

    How do you avoid template ambiguity when working with templates and the class keyword. consider this example where the templated item is ambiguous (I assume) because non-number values can be passed into the function that accepts the templated item.


    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    template<class X>
    long double Add(X * FirstNum, X * SecondNum);
    
    int main(void) {
    	float W;
    	int Z;
    	cout << "Enter a value for w " << endl;
    	cin >> W;
    
    	cout << "Enter a value for z " << endl;
    	cin >> Z;
    
    	cout << "The value of X plus Y is " << Add(&W, &Z) << endl;
    	getch();
    	return 0;
    }
    
    template<class X>
    long double Add(X * FirstNum, X * SecondNum) {
    	long double temp = *FirstNum + *SecondNum;
    	return temp;
    }
    However I have never actually had problems using templates whether it be with vectors or otherwise. It is just that I have never written a linked list with templates and I figured it time to do so (doesn't ever C++ programmer do that during his/her life?)

    EDIT:
    Oh and by the way this program was written as an example so we need not comment on anything other than templates (unless I did something blatantly wrong that will make the program not compile anyway, which I don't think I did).
    Last edited by Shadow12345; 11-09-2002 at 12:03 PM.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    It doesn't know whether to call

    Code:
    Add(float*, float*) 
    Add(int*, int*)
    I'd suggest that you

    A. take the items by const reference, not by pointer.
    B. call the function like this

    Code:
    Add<float>(w,z);
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    In your example, What class template do you instantiate? I do not see a class template. I see a template function.

    Implement the template class with template functions.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 05:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 09:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM

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