Thread: template trouble

  1. #1
    Registered User
    Join Date
    Aug 2001

    template trouble


    I've encountered a problem concerning templates, perhaps someone knows how to fix this..

    I have a template class "template<class T> class myclass"
    this class has a memberfunction that is supposed to take as a parameter an instance of the class:

    template<class T> ret myclass<T>::myfunc( myclass<T> )

    so far so good.. but now for the part I don't get.. The functions parameter is to be the class with an arbitrary type .. not necessarily the type of the 'this' class...

    something like this.. just to make you see what I wan't..

    template<class T, class T1> ret myclass<T>::myfunc( myclass<T1> )

    The above statement doesn't do the trick so how should I write..

    - Anders

  2. #2
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    If you want the class to have two template types you have to put both of them every time you refer to to the class name. So adding a second template type won't work, because each instantiation would still have its arguments be of its own type anyway.

    I think the easiest thing to do would just to put up with multiple overloads and try this:

    template<class T> ret myclass<T>::myfunc( myclass<int> )

    template<class T> ret myclass<T>::myfunc( myclass<float> )

    template<class T> ret myclass<T>::myfunc( myclass<double> )

    ... and so on.

    Also, most compilers let you template individual member functions. (The one I was using this summer didn't, though.) So there might be a slightly more advanced way as well.
    Last edited by Procyon; 09-01-2001 at 03:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Template Trouble - Brainache Please Help
    By SpaceCadet in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2006, 08:51 AM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04: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