Using comparing operetors on variables defined by Templates

This is a discussion on Using comparing operetors on variables defined by Templates within the C++ Programming forums, part of the General Programming Boards category; I cant figure out the syntax for how to create new operators such as '<', '>', '==', etc. for variables ...

  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
    zen
    zen is offline
    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, 08:39 AM
  3. Replies: 7
    Last Post: 04-13-2003, 11: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

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