code_junkie

Problem Linking “static” Methods in C++

Nov 11th, 2011
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. // CppClass.cpp
  2. #include <iostream>
  3. #include <pthread.h>
  4.  
  5. static pthread_t thread;
  6. static pthread_mutex_t mutex;
  7. static pthread_cond_t cond;
  8. static int shutdown;
  9.  
  10. using namespace std;
  11.  
  12. class CppClass
  13. {
  14. public:
  15. static void Start()
  16. {
  17. cout << "Testing start function." << endl;
  18. shutdown = 0;
  19. pthread_attr_t attr;
  20. pthread_attr_init(&attr);
  21. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  22. pthread_mutex_init(&mutex, NULL);
  23. pthread_cond_init(&cond, NULL);
  24.  
  25. pthread_create(&thread, &attr, run_thread, NULL);
  26. }
  27.  
  28. static void Stop()
  29. {
  30. pthread_mutex_lock(&mutex);
  31. shutdown = 1;
  32. pthread_cond_broadcast(&cond);
  33. pthread_mutex_unlock(&mutex);
  34. }
  35.  
  36. static void Join()
  37. {
  38. pthread_join(thread, NULL);
  39. }
  40. private:
  41. static void *run_thread(void *pthread_args)
  42. {
  43. CppClass *obj = new CppClass();
  44. pthread_mutex_lock(&mutex);
  45. while (shutdown == 0)
  46. {
  47. struct timespec ts;
  48. ts.tv_sec = time(NULL) + 3;
  49. pthread_cond_timedwait(&cond, &mutex, &ts);
  50. if (shutdown)
  51. {
  52. break;
  53. }
  54. obj->display();
  55. }
  56. pthread_mutex_unlock(&mutex);
  57. pthread_mutex_destroy(&mutex);
  58. pthread_cond_destroy(&cond);
  59. pthread_exit(NULL);
  60. return NULL;
  61. }
  62.  
  63. void display()
  64. {
  65. cout << " Inside display() " << endl;
  66. }
  67. };
  68.  
  69. // main.cpp
  70. #include <iostream>
  71. /*
  72. * If I remove the comment below and delete the
  73. * the class declaration part, it works.
  74. */
  75. // #include "CppClass.cpp"
  76. using namespace std;
  77.  
  78. class CppClass
  79. {
  80. public:
  81. static void Start();
  82. static void Stop();
  83. static void Join();
  84. };
  85.  
  86. int main()
  87. {
  88. CppClass::Start();
  89. while (1)
  90. {
  91. int quit;
  92. cout << "Do you want to end?: (0 = stay, 1 = quit) ";
  93. cin >> quit;
  94. cout << "Input: " << quit << endl;
  95. if (quit)
  96. {
  97. CppClass::Stop();
  98. cout << "Joining CppClass..." << endl;
  99. CppClass::Join();
  100. break;
  101. }
  102. }
  103. }
  104.  
  105. class NewClass {
  106. public:
  107. NewClass();
  108. int methodA();
  109. int methodB();
  110. }; <- don't forget the semicolon
  111.  
  112. class NewClass {
  113. public:
  114. NewClass();
  115. int methodA();
  116. int methodB();
  117. }; <- don't forget the semicolon
  118.  
  119. #include "NewClass.h"
  120.  
  121. NewClass::NewClass() {
  122. // constructor goes here
  123. }
  124.  
  125. int NewClass::methodA() {
  126. // methodA goes here
  127. return 0;
  128. }
  129.  
  130. int NewClass::methodB() {
  131. // methodB goes here
  132. return 1;
  133. }
  134.  
  135. #include "NewClass.h"
  136.  
  137. int main() {
  138. NewClass nc;
  139. // do something with nc
  140. }
  141.  
  142. // CppClass.h
  143. using namespace std;
  144.  
  145. class CppClass
  146. {
  147. public:
  148. static void Start();
  149. static void Stop();
  150. static void Join();
  151. private:
  152. void *run_thread(void *pthread_args);
  153. void display();
  154. };
  155.  
  156. // CppClass.cpp
  157. #include <iostream>
  158. #include <pthread.h>
  159. #include "CppClass.h"
  160.  
  161. using namespace std;
  162.  
  163. void CppClass::Start()
  164. {
  165. /* method body goes here */
  166. }
  167. void CppClass::Stop()
  168. {
  169. /* method body goes here */
  170. }
  171. void CppClass::Join()
  172. {
  173. /* method body goes here */
  174. }
  175. void *CppClass::run_thread(void *pthread_args)
  176. {
  177. /* method body goes here */
  178. }
  179. void CppClass::display() {
  180. /* method body goes here */
  181. }
  182.  
  183. // main.cpp
  184. #include "CppClass.h"
  185.  
  186. int main()
  187. {
  188. /* main method body here */
  189. }
Add Comment
Please, Sign In to add comment