Thread: Using comparing operetors on variables defined by Templates

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    14

    Using comparing operetors on variables defined by Templates

    I cant figure out the syntax for how to create new operators such as '<', '>', '==', etc. for variables defined by Templates

    for example:

    /////////////////////////////////////////////////////////////////////////////
    template <class T>
    class Whatever
    {
    private:
    T tData;
    public:
    tData GetLargest(tData, tData);
    };

    tData Whatever<T>::GetLargest(tData tD1, tData tD2)
    {
    if(tD1 > tD2) return tD1;
    else return tD2;
    }

    void main(vodi)
    {
    Whatever <unsigned int> W;
    W.GetLargers(2, 4);
    }

    /////////////////////////////////////////////////////////////////////////////

    Now this will work since '<' is not defined for this type, so my question is, how do i make my own operator when it has to do
    with templates?

    thx

    /Laos
    Last edited by Laos; 12-21-2001 at 12:06 PM.

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    As long as < is defined for the data type when the template is instantiated then you can call GetLargest(). Your class isn't working because your confusing instances of variables with their types -


    Code:
    #include <iostream>
    
    using namespace std;
    
    template <class T> 
    class Whatever 
    { 
    	private: 
    	T tData; 
    public: 
    	T GetLargest(T t1, T t2); 
    }; 
    
    template<class T>
    T Whatever<T>::GetLargest(T tD1, T tD2) 
    { 
    	if(tD1 > tD2) return tD1; 
    	else return tD2; 
    } 
    
    int main() 
    { 
    	Whatever <unsigned int> W; 
    	cout << W.GetLargest(2,4); 
    	return 0;
    }
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 07:16 AM
  2. too many variables defined
    By campermama in forum C++ Programming
    Replies: 5
    Last Post: 06-27-2004, 07:39 AM
  3. Replies: 7
    Last Post: 04-13-2003, 10:53 PM
  4. Undefined Variables that ARE defined
    By Teema in forum C Programming
    Replies: 3
    Last Post: 12-03-2002, 01:14 AM
  5. Header files
    By borland_man in forum C++ Programming
    Replies: 14
    Last Post: 02-22-2002, 04:30 AM