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

Thread class: need help!

P: 1
Dear experts, you help is necessary!
When trying to compile this code an error encounted.
I think that thread in class is good idea for c++, so if you know what`s wrong, please post your opinion!
(and sorry for my broken english ;)


Expand|Select|Wrap|Line Numbers
  1. #include <windows.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #include <memory.h>
  5. #include <tchar.h>
  6.  
  7. template <class T> class Thread
  8. {
  9. public:
  10.     Thread(){}
  11.     Thread(T data){ this->data = data; }
  12.     virtual ~Thread(){}
  13.     virtual void Proc(T data);
  14.  
  15.     void Run() {
  16.         Run(data);
  17.     }
  18.  
  19.     void Run(T data) {
  20.         this->data = data;
  21.         handle = ::CreateThread(0, 0,(LPTHREAD_START_ROUTINE)ThreadFunc, (LPVOID)this, 0, 0);
  22.     }
  23.  
  24.     void Join() {
  25.         ::WaitForSingleObject(handle, INFINITE);
  26.     }
  27. private:
  28.     HANDLE handle;
  29.     T data;
  30.     static DWORD WINAPI ThreadFunc(LPVOID param)
  31.     {
  32.         Thread* This = (Thread*)param;
  33.         This->Proc(This->data);
  34.         return 0;
  35.     }
  36. };
  37.  
  38.  
  39.  
  40. class MyTask : public Thread<wchar_t*>
  41. {
  42.     void Proc(wchar_t* data)
  43.     {
  44.         ::MessageBox(NULL, L"Message from thread", data, NULL);
  45.     }
  46. };
  47.  
  48.  
  49. int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE, char*, int iShowCmd)
  50. {
  51.     MyTask * t = new MyTask();
  52.     t->Run(L"Hola!");
  53.     t->Join();
  54.     return 0;
  55. }
  56.  
Expand|Select|Wrap|Line Numbers
  1. Visual C++ 2008, Express Edition:
  2.  
  3. 1>App.obj : error LNK2001: unresolved external symbol ""public: virtual void __thiscall Thread<wchar_t *>::Proc(wchar_t *)" (?Proc@?$Thread@PA_W@@UAEXPA_W@Z)"
  4.  
Any ideas?
May 30 '09 #1
Share this Question
Share on Google+
1 Reply


100+
P: 687
Because you haven't implemented Thread::Proc. You did it with MyTask::Proc, but not for parent template class. Either decalare it pure virtual or define with empty body.
May 31 '09 #2

Post your reply

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