473,399 Members | 2,478 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,399 software developers and data experts.

HELP can not link my template class

2
hi all. i have a template class that i can not link. there is no problem when i add the method def's inside the header file but as soon as i move them out i get a link error where i define the object. i have tryed a simple test of the same code and it links fine so am baffeled at what is happing

this is my simple test that complies and links


Expand|Select|Wrap|Line Numbers
  1. bool test();
  2.  
  3. template <class  T>
  4. class queue
  5. {
  6. public:
  7.     // construct a queue
  8.     queue(int item_size, int queue_len);
  9.     // deconstructor
  10.  
  11.     ~queue(){};
  12.  
  13.     // post an item onto the queue
  14.     bool post(T* item);
  15.  
  16.     // get next item from queuw
  17.  
  18.     bool get(T* item);
  19.  
  20.     // wait for timeout for item
  21.  
  22.     bool get_wait(T* item, unsigned int timeout = 0);
  23.  
  24.     int item_queued(T* item);
  25.  
  26. private:
  27.     int _que;
  28. };
  29.  
  30. template <class T>
  31. queue<T>::queue(int item_size, int queue_len)
  32. {
  33.     test();
  34. };
  35.  
  36. template <class T>
  37. bool queue<T>::post(T* item)
  38. {
  39.     return test();
  40. }
  41.  
  42. // return the current value of the semaphore count
  43. template <class T>
  44. bool queue<T>::get(T* item)
  45. {
  46.     return test();
  47. }
  48.  
  49. // wait for the semaphore count to go above zero
  50. template <class T>
  51. bool queue<T>::get_wait(T* item, unsigned int timeout)
  52. {
  53.     return test();
  54. }
  55.  
  56. template <class T>
  57. int queue<T>::item_queued(T* item)
  58. {
  59.     return test();
  60. };
  61.  
  62. bool test()
  63. {
  64.     return true;
  65. }
  66.  
  67. int main ()
  68. {
  69.  
  70.     queue<int> myqueue(2,3);
  71.     return 1;
  72. }
  73.  this is the file that wont link
  74.  
  75.  
  76. namespace os {
  77.  
  78. // CLASSES ///////////////////////////////////////////////////////////////////
  79.  
  80. // abstraction of an operating system semaphore, allowing the counting of 
  81. // resources between threads.  
  82.  
  83. class semaphore
  84. {
  85. public:
  86.     // construct a semaphore
  87.  
  88.     semaphore(int value = 0);
  89.  
  90.     // deconstructor
  91.  
  92.     ~semaphore();
  93.  
  94.     // increment the semaphore count by one.  if it was previously non-zero 
  95.     // and someone was waiting on it, the waiting thread will be woken up
  96.  
  97.     void post();
  98.  
  99.     // return the current value of the semaphore count
  100.  
  101.     int get();
  102.  
  103.     // wait for the semaphore count to go above zero
  104.  
  105.     bool wait(unsigned int timeout = 0);
  106.  
  107.     // acquire semaphore lock if count is above zero and return true. if lock
  108.     // not available returns false immediately
  109.  
  110.     bool try_wait();
  111.  
  112. private:
  113.     ak_sem_t _sem;
  114. };
  115.  
  116. template <class  T>
  117. class queue
  118. {
  119. public:
  120.     // construct a queue
  121.     queue(int item_size, int queue_len);
  122.  
  123.     // deconstructor
  124.  
  125.     ~queue(){};
  126.  
  127.     // post an item onto the queue
  128.     bool post(T* item);
  129.  
  130.     // get next item from queuw
  131.  
  132.     bool get(T* item);
  133.  
  134.  
  135.     // wait for timeout for item
  136.  
  137.     bool get_wait(T* item, unsigned int timeout = 0);
  138.  
  139.  
  140.     int items_queued();
  141.  
  142. private:
  143.     ak_queu_t _que;
  144. };
  145. // NAMESPACE END /////////////////////////////////////////////////////////////
  146.  
  147. };
  148.  
  149. .cpp file is
  150. semaphore::semaphore(int value)
  151. {
  152.     ak_sem_create(&_sem, value);
  153. }
  154.  
  155. // deconstructor
  156.  
  157. semaphore::~semaphore()
  158. {
  159.     ak_sem_free(&_sem);
  160. }
  161.  
  162. // increment the semaphore count by one.  if it was previously non-zero 
  163. // and someone was waiting on it, the waiting thread will be woken up
  164.  
  165. void semaphore::post()
  166. {
  167.     ak_sem_post(&_sem);
  168. }
  169.  
  170. // return the current value of the semaphore count
  171.  
  172. int semaphore::get()
  173. {
  174.     return ak_sem_get(&_sem);
  175. }
  176.  
  177. // wait for the semaphore count to go above zero
  178.  
  179. bool semaphore::wait(unsigned int timeout)
  180. {
  181.     return ak_sem_wait(&_sem, timeout) == 1 ? true : false;
  182. }
  183.  
  184. // acquire semaphore lock if count is above zero and return true. if lock
  185. // not available returns false immediately
  186.  
  187. bool semaphore::try_wait()
  188. {
  189.     return ak_sem_trywait(&_sem) == 1 ? true : false;
  190. }
  191.  
  192. //////////////////////////////////////////////////////////////
  193. // increment the semaphore count by one.  if it was previously non-zero 
  194. // and someone was waiting on it, the waiting thread will be woken up
  195.  
  196. template <class T>
  197. queue<T>::queue(int item_size, int queue_len)
  198. {
  199.     ak_que_setup(&_que, item_size, queue_len);
  200. };
  201.  
  202. template <class T>
  203. bool queue<T>::post(T* item)
  204. {
  205.     return (bool)ak_que_post(_que,(const void*)item);
  206. }
  207.  
  208. // return the current value of the semaphore count
  209. template <class T>
  210. bool queue<T>::get(T* item)
  211. {
  212.     return (bool)ak_que_get(_que, (void*)item);
  213. }
  214.  
  215. // wait for the semaphore count to go above zero
  216. template <class T>
  217. bool queue<T>::get_wait(T* item, unsigned int timeout)
  218. {
  219.     return ak_que_wait(_que, (void*)item, timeout) == 1 ? true : false;
  220. }
  221.  
  222. template <class T>
  223. int queue<T>::items_queued()
  224. {
  225.     return ak_que_get_num_items(_que);
  226. };
  227.  
the sem class complies and links fine jsut not the template class.

please help. very stuck
Nov 13 '08 #1
1 2108
Banfa
9,065 Expert Mod 8TB
The definitions of the methods of a template class have to be in the header file with the class declaration, or at least must be included at the same time that the class is included.

Remember the template class is not a class but a pattern that instructs the compiler how to produce a class once the template parameters are given. If the method definitions are not there at the time the code uses the class and thus the compiler tries to create a class following the template then the compiler can not create a complete class so you get errors.


Also please use [code],,,[/code] tags round your posted code, it makes it more readable.
Nov 14 '08 #2

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

Similar topics

0
by: Mike | last post by:
I need to have some links within an xml document to external pages. I have tried with the below xml and xsl documents(the proposed link has a tag called link under paragraph): XML:...
5
by: Adam | last post by:
Hi, Me very confused. I have some XML that I want to convert to a more basic XML. I have put an example of what I have and what I want, I have used XSL to convert XML to HTML, but never this way. ...
2
by: dinks | last post by:
Hi, I'm new to C++ and have been assigned a task which i dont completely understand. Any help would be greately appreciated. Here is the problem: The class "linkedListType" use the "assert"...
1
by: David2511 | last post by:
Hello, I need a little help. I try to write the following architecture : an abstract template class A Two classes derived from class A : the classes B and C which are concrete. The class...
1
by: Mario Rosario | last post by:
Hi, Thanks in advance for your help. I have a compile problem; actually a link problem, when I compile the class template below. I would appreciate if anyone can tell me what is wrong and how...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
2
by: adMjb | last post by:
Hi Any help would be fantastic I have a simple problem but I cant work it out... DOHH...., this is my XML: <?xml version="1.0" encoding="UTF-8" ?> <?xml-stylesheet type="text/css"...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
by: magicofureyes | last post by:
Hello Guys im a just a new user and i dnt knw much abt Xml i want to upload a new template in Blogger so got some free coding but when i save this code in Blogger template it say '''' Your...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.