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 -
bool test();
-
-
template <class T>
-
class queue
-
{
-
public:
-
// construct a queue
-
queue(int item_size, int queue_len);
-
// deconstructor
-
-
~queue(){};
-
-
// post an item onto the queue
-
bool post(T* item);
-
-
// get next item from queuw
-
-
bool get(T* item);
-
-
// wait for timeout for item
-
-
bool get_wait(T* item, unsigned int timeout = 0);
-
-
int item_queued(T* item);
-
-
private:
-
int _que;
-
};
-
-
template <class T>
-
queue<T>::queue(int item_size, int queue_len)
-
{
-
test();
-
};
-
-
template <class T>
-
bool queue<T>::post(T* item)
-
{
-
return test();
-
}
-
-
// return the current value of the semaphore count
-
template <class T>
-
bool queue<T>::get(T* item)
-
{
-
return test();
-
}
-
-
// wait for the semaphore count to go above zero
-
template <class T>
-
bool queue<T>::get_wait(T* item, unsigned int timeout)
-
{
-
return test();
-
}
-
-
template <class T>
-
int queue<T>::item_queued(T* item)
-
{
-
return test();
-
};
-
-
bool test()
-
{
-
return true;
-
}
-
-
int main ()
-
{
-
-
queue<int> myqueue(2,3);
-
return 1;
-
}
-
this is the file that wont link
-
-
-
namespace os {
-
-
// CLASSES ///////////////////////////////////////////////////////////////////
-
-
// abstraction of an operating system semaphore, allowing the counting of
-
// resources between threads.
-
-
class semaphore
-
{
-
public:
-
// construct a semaphore
-
-
semaphore(int value = 0);
-
-
// deconstructor
-
-
~semaphore();
-
-
// increment the semaphore count by one. if it was previously non-zero
-
// and someone was waiting on it, the waiting thread will be woken up
-
-
void post();
-
-
// return the current value of the semaphore count
-
-
int get();
-
-
// wait for the semaphore count to go above zero
-
-
bool wait(unsigned int timeout = 0);
-
-
// acquire semaphore lock if count is above zero and return true. if lock
-
// not available returns false immediately
-
-
bool try_wait();
-
-
private:
-
ak_sem_t _sem;
-
};
-
-
template <class T>
-
class queue
-
{
-
public:
-
// construct a queue
-
queue(int item_size, int queue_len);
-
-
// deconstructor
-
-
~queue(){};
-
-
// post an item onto the queue
-
bool post(T* item);
-
-
// get next item from queuw
-
-
bool get(T* item);
-
-
-
// wait for timeout for item
-
-
bool get_wait(T* item, unsigned int timeout = 0);
-
-
-
int items_queued();
-
-
private:
-
ak_queu_t _que;
-
};
-
// NAMESPACE END /////////////////////////////////////////////////////////////
-
-
};
-
-
.cpp file is
-
semaphore::semaphore(int value)
-
{
-
ak_sem_create(&_sem, value);
-
}
-
-
// deconstructor
-
-
semaphore::~semaphore()
-
{
-
ak_sem_free(&_sem);
-
}
-
-
// increment the semaphore count by one. if it was previously non-zero
-
// and someone was waiting on it, the waiting thread will be woken up
-
-
void semaphore::post()
-
{
-
ak_sem_post(&_sem);
-
}
-
-
// return the current value of the semaphore count
-
-
int semaphore::get()
-
{
-
return ak_sem_get(&_sem);
-
}
-
-
// wait for the semaphore count to go above zero
-
-
bool semaphore::wait(unsigned int timeout)
-
{
-
return ak_sem_wait(&_sem, timeout) == 1 ? true : false;
-
}
-
-
// acquire semaphore lock if count is above zero and return true. if lock
-
// not available returns false immediately
-
-
bool semaphore::try_wait()
-
{
-
return ak_sem_trywait(&_sem) == 1 ? true : false;
-
}
-
-
//////////////////////////////////////////////////////////////
-
// increment the semaphore count by one. if it was previously non-zero
-
// and someone was waiting on it, the waiting thread will be woken up
-
-
template <class T>
-
queue<T>::queue(int item_size, int queue_len)
-
{
-
ak_que_setup(&_que, item_size, queue_len);
-
};
-
-
template <class T>
-
bool queue<T>::post(T* item)
-
{
-
return (bool)ak_que_post(_que,(const void*)item);
-
}
-
-
// return the current value of the semaphore count
-
template <class T>
-
bool queue<T>::get(T* item)
-
{
-
return (bool)ak_que_get(_que, (void*)item);
-
}
-
-
// wait for the semaphore count to go above zero
-
template <class T>
-
bool queue<T>::get_wait(T* item, unsigned int timeout)
-
{
-
return ak_que_wait(_que, (void*)item, timeout) == 1 ? true : false;
-
}
-
-
template <class T>
-
int queue<T>::items_queued()
-
{
-
return ak_que_get_num_items(_que);
-
};
-
the sem class complies and links fine jsut not the template class.
please help. very stuck
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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:...
|
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.
...
|
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"...
|
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...
|
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...
|
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...
|
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"...
|
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...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |