By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,601 Members | 1,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,601 IT Pros & Developers. It's quick & easy.

class template with static member functions problem

P: 53
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 )
Expand|Select|Wrap|Line Numbers
  1. template<class T>
  2. class DllExport Helper
  3. {
  4.         public:
  5.  
  6.             LEX_INLINE static T max(T Value1, T Value2);
  7.  
  8.             LEX_INLINE static void setNaN(T &Value);
  9. };
  10.  
  11. template<class T>
  12.         LEX_INLINE T Helper<T>::max(T Value1, T Value2)
  13.         {
  14.             return (Value1 < Value2) ? Value2 : Value1;
  15.         }
  16.  
  17.         template<class T>
  18.         LEX_INLINE void Helper<T>::setNaN(T &Value)
  19.         {
  20.             Value = sqrt(-1.0f);
  21.         }
now for example if i try this in a different file, for example -> Helper::max(1.0f, 2.0f)

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

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


Any ideas for this rusty programmer?
Nov 25 '10 #1
Share this Question
Share on Google+
5 Replies


Banfa
Expert Mod 5K+
P: 8,916
When you use a template class you have to provide the template parameters so Helper::max(1.0f, 2.0f) should be Helper<float>::max(1.0f, 2.0f)
Nov 25 '10 #2

P: 53
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::Helper' : use of class template requires template argument list"

but it did remove the "Error 10 error C2670: 'Lex::Maths::Helper<T>::setNaN' : the function template cannot convert parameter 1 from type" errors
Nov 25 '10 #3

Banfa
Expert Mod 5K+
P: 8,916
This (which is basically your code) compiles for me

Expand|Select|Wrap|Line Numbers
  1. #include <cmath>
  2. using namespace std;
  3.  
  4. #define DllExport
  5. #define LEX_INLINE inline
  6.  
  7. template<class T>
  8. class DllExport Helper
  9. {
  10.     public:
  11.  
  12.         LEX_INLINE static T max(T Value1, T Value2);
  13.  
  14.         LEX_INLINE static void setNaN(T &Value);
  15. };
  16.  
  17. template<class T>
  18. LEX_INLINE T Helper<T>::max(T Value1, T Value2)
  19. {
  20.     return (Value1 < Value2) ? Value2 : Value1;
  21. }
  22.  
  23. template<class T>
  24. LEX_INLINE void Helper<T>::setNaN(T &Value)
  25. {
  26.     Value = sqrt(-1.0f);
  27. }
  28.  
  29. int main()
  30. {
  31.     float t = Helper<float>::max(1.0f, 2.0f);
  32.  
  33.     return 0;
  34. }
  35.  
producing the single warning

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

for obvious reasons.
Nov 25 '10 #4

P: 53
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
Nov 25 '10 #5

P: 53
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 :)
Nov 25 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.