473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 )
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
5 1958
Banfa
9,065 Expert Mod 8TB
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
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
9,065 Expert Mod 8TB
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
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
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

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

Similar topics

5
by: Naren | last post by:
Hello Grp, Correct me if I am wrong. static member functions can act only on static member varaibles.It can accessed by using the name of the class. Then why is there an access controller. ...
3
by: exits funnel | last post by:
Hello, One of the problems at the end of Chapter 14 in Bruce Eckel's thinking in C++ reads as follows: Create a class with two static member functions. Inherit from this class and redefine...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
3
by: qWake | last post by:
The C++ language standard stipulates at section 9.4.1 that " A static member function shall not be declared const " The question is: what problem(s) could possibly exist in allowing static...
1
by: RainerFaulstich | last post by:
Hi, Some dummy question : Are static member functions of a class indeed a single instance for all instances of the class or have difference instances of this class have its own instances of this...
3
by: paul.furber | last post by:
Hi all, I have some code which looks a bit like this: #define Offset(m, T) ((size_t)(&((T *)1)->m) - 1) class Point: private: int *x,*y;
0
by: Axter | last post by:
I'm currently working on the following policy base smart pointer: http://code.axter.com/smart_ptr.h Before working on the above code, I read the following links:...
6
by: Olumide | last post by:
Hi - I've got a class that contains static member functions alone, all of whose arguments are passed by reference as shown below: class MySpiffyClass{ // no constructor, destructor or...
5
by: Ian Collins | last post by:
Consider the following snippet: struct X { static bool called; }; struct Y : X { Y() { called = true; } // g++ is happy with this }; template <int N>
5
by: chgans | last post by:
Hi all, I'm having difficulties with some template static member, especially when this member is a template instance, for example: ---- template<typename T> class BaseT { public: static...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.