C++ STL Memory Allocator Compile Error -
I am writing C ++ custom allocation for use with STL when I enter the following code in class definition It is compiled:
#include "MyAlloc.hpp" #if 1 template & lt; Typename T & gt; Type Anonymous MyLock & lt; T & gt; :: indicator MyAlloc & lt; T & gt; :: assign (size_type n, myloc & lt; void & gt; :: const_pointer p) {void * ptr = getmemory (n * sizeof (t)); Type Anonymous Mylock & lt; T & gt; :: Indicator tptr = static_cast & lt; MyAlloc & lt; T & gt; :: Pointer & gt; (PTR); Return TPTR; } #endif
But when I put it in a separate .cpp file, I get the following error. What am I doing wrong? The error is on static_cast line. Member Function in 'typename MyAlloc & lt;
g ++ -c MyAlloc.cpp MyAlloc.cpp: T & gt; :: indicator MyAlloc & lt; T & gt; :: Allocated (size_t, Constant Wide *): 'MyAlloc.cpp: 9: Error: Expected Type-Specifier MyAlloc.cpp: 9: Error: Expected `& gt;' MyAlloc.cpp: 9: Error: Expected `('MyAlloc.cpp: 9: Error: Expected') 'First'; ' Token form: *** [MyAlloc.o] Error 1
PT
The template should always be defined within a translation unit. To use the template function, the definition of the template should be in the header file, no different. Not cpp file
Comments
Post a Comment