class template with static member functions problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • themadme
    New Member
    • Mar 2007
    • 53

    class template with static member functions problem

    Hi there,


    I'm having a little bit of a problem and hopefully some one can help me.

    I have not programmed in c++ for a while so excuse me for being silly

    My problem involves with me creating a template class with static inlined member functions.

    E.g.

    LEX_INLINE is just a typedef for __forceinline
    DllExport is a typedef for __declspec( dllexport )
    Code:
    template<class T>
    class DllExport Helper
    {
    		public:
    
    			LEX_INLINE static T max(T Value1, T Value2);
    
    			LEX_INLINE static void setNaN(T &Value);
    };
    
    template<class T>
    		LEX_INLINE T Helper<T>::max(T Value1, T Value2)
    		{
    			return (Value1 < Value2) ? Value2 : Value1;
    		}
    
    		template<class T>
    		LEX_INLINE void Helper<T>::setNaN(T &Value)
    		{
    			Value = sqrt(-1.0f);
    		}
    now for example if i try this in a different file, for example -> Helper::max(1.0 f, 2.0f)

    i get compile error saying "Error 8 error C2955: 'Lex::Maths::He lper' : use of class template requires template argument list"

    also i get errors for other functions saying this -> "Error 10 error C2670: 'Lex::Maths::He lper<T>::setNaN ' : the function template cannot convert parameter 1 from type"


    Any ideas for this rusty programmer?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    When you use a template class you have to provide the template parameters so Helper::max(1.0 f, 2.0f) should be Helper<float>:: max(1.0f, 2.0f)

    Comment

    • themadme
      New Member
      • Mar 2007
      • 53

      #3
      thanks for the reply banfa but sadly to say, i have already done this and i still receive the "Error 8 error C2955: 'Lex::Maths::He lper' : use of class template requires template argument list"

      but it did remove the "Error 10 error C2670: 'Lex::Maths::He lper<T>::setNaN ' : the function template cannot convert parameter 1 from type" errors

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        This (which is basically your code) compiles for me

        Code:
        #include <cmath>
        using namespace std;
        
        #define DllExport
        #define LEX_INLINE inline
        
        template<class T>
        class DllExport Helper
        {
        	public:
        
        		LEX_INLINE static T max(T Value1, T Value2);
        
        		LEX_INLINE static void setNaN(T &Value);
        };
         
        template<class T>
        LEX_INLINE T Helper<T>::max(T Value1, T Value2)
        {
        	return (Value1 < Value2) ? Value2 : Value1;
        }
        
        template<class T>
        LEX_INLINE void Helper<T>::setNaN(T &Value)
        {
        	Value = sqrt(-1.0f);
        }
        
        int main()
        {
        	float t = Helper<float>::max(1.0f, 2.0f);
        
        	return 0;
        }
        producing the single warning

        bytes.cpp:31: warning: unused variable 't'

        for obvious reasons.

        Comment

        • themadme
          New Member
          • Mar 2007
          • 53

          #5
          hmmm interesting .. why am i still getting errors. Im going to do a few more tests tonight and find out why im getting errors. Thanks for the reply

          Comment

          • themadme
            New Member
            • Mar 2007
            • 53

            #6
            Started a new clean slate of code and now it works ... i have no idea what sill mistake i did previously lol. Oh well thanks for your help Banfa :)

            Comment

            Working...