   Base Template class from extended class initialization problem
  Hi, I've problem initializing base class from an extended one.Header file:#include <windows.h>template <typename DERIVED_TYPE>class CBaseWindow{public:static LRESULT CALLBACK WinProc (HWND hWindow, UINT uMsg, WPARAM wParam, LPARAM lParam){DERIVED_TYPE* pThis = NULL;if (uMsg == WM_NCCREATE){CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;pThis = (DERIVED_TYPE*)pCreate->lpCreateParams;SetWindowLongPtr(hWindow, GWLP_USERDATA, (LONG_PTR)pThis);pThis->m_hWindow = hWindow;}else{pThis = (DERIVED_TYPE*)GetWindowLongPtr(hWindow, GWLP_USERDATA);}if (pThis){return DefWindowProc(hWindow, uMsg, wParam, lParam);}else{return DefWindowProc(hWindow, uMsg, wParam, lParam);}}BOOL Create ();CBaseWindow() : m_hWindow(NULL) {}HWND Window () const { return m_hWindow; }protected:HWND m_hWindow;};class CWindow : public CBaseWindow<CWindow>{public:CWindow() : CBaseWindow() {}};CPP file:#include "MWindows.h"/*-------------------------------------------------------------------------------------------//    ////   These are methods from CBaseWindow  ////    //-------------------------------------------------------------------------------------------*///-------------------------------------------------------------------------------------------//Name: Create//Desc: Initializes class and window//-------------------------------------------------------------------------------------------template <typename DERIVED_TYPE> BOOL CBaseWindow<DERIVED_TYPE>::Create (){WNDCLASSEX W;ZeroMemory(&W, sizeof(W));W.cbSize = sizeof(W);W.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(0, 0, 0));W.lpfnWndProc = DERIVED_TYPE::WinProc;W.lpszClassName = L"CLASSNAME";RegisterClassEx(&W);HWND m_hWindow = CreateWindowEx(NULL, L"CLASSNAME", L"WINDOWNAME", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, GetSystemMetrics(SM_CXSCREEN) / 2, GetSystemMetrics(SM_CYSCREEN) / 2, NULL, NULL, GetModuleHandle(NULL), this);return (m_hWindow ? TRUE : FALSE);}In main I write: CWindow W, and get an error:Error1error LNK2019: unresolved external symbol "public: int __thiscall CBaseWindow<class CWindow>::Create(void)" (?Create@?$CBaseWindow@VCWindow@@@@QAEHXZ) referenced in function _wWinMain@16WinMain.objMWindowsI know, that if I use CBaseWindow directly I should write something like:CBaseWindow<typeofclass> W;But how am I supposed to do this through extended class?   
  Do you have BOOL Create() defined anywhere?  I'm just seeing the declaration in the header.[edit]Never mind, I looked at that all wrong.   ;  The problem is you've got a templated class, and you've tried to put the member functions in a cpp file.  Very few compilers support this (do a search about the export command).  Try moving Create() into the header or inline it in the class.   ;  Is it okay to write everything in header file? How do real programmers implement template classes?   ;Quote:Original post by MarusuIs it okay to write everything in header file? How do real programmers implement template classes?It's not a matter of okay, it's a matter of that is the way you have to do it.  What you have to be careful of, since you have to do this, is circular references between headers.[edit]Very few compilers ever implemented the export command, which was required to do it the way you had it.  I believe they have even removed it from the C++0x standard, due to the lack of adoption (someone correct me on this if I'm wrong).   ;Quote:Original post by MarusuIs it okay to write everything in header file? How do real programmers implement template classes?Generally, you want to define all template code in the same header file in which it's declared. This includes template functions, template member functions, member functions of template classes, and (this one just kills me) template member functions of template classes.So yes, it's okay :)   