|
-
January 21st, 2004, 06:18 AM
#1
Template function
Hello there,
My application has several different kinds of linked lists, of which all are created dynamically using "new".
I wish to create a template function that will free up the memory when finished with the linked lists.
Is it possible to create a template function that will take different pointers to structures (ie, nodes). And what is the format for it??
Thanks in advance.
-
January 21st, 2004, 07:00 AM
#2
You should make a template for your linked list class. Any C++ book/tutorial should have a section on that.
You should also ask yourself why to make a template for lists, if there is already one in the standard C++ library (called list) which you can simply use.
-
January 21st, 2004, 07:03 AM
#3
I'm not sure what are you looking for, but you can always:
template <class T1, class T2, ...>
void foo(T1 *p1, T2 *p2, ...)
{
//whatever stuff.
}
-
January 21st, 2004, 08:40 AM
#4
AdDav,
I have a template function as follows:
template <class Type> void Delete(Type * list)
{
//do something
}
and its prototype in the header file is:
template <class Type> Delete(Type);
And I call the function as follows:
Delete(list_head);//list head is a pointer to a structure
I get an unresolved external:
int __cdecl Delete(struct V_FSM *)" (?Delete@@YAHPAUV_FSM@@@Z)
Whats wrong with this code?
-
January 21st, 2004, 08:41 AM
#5
What's wrong with std::list<>?
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
January 21st, 2004, 08:55 AM
#6
I have alot of coding done using my own lists. I did not know about the std::list.
-
January 21st, 2004, 05:57 PM
#7
Originally posted by Fergal21
AdDav,
I have a template function as follows:
template <class Type> void Delete(Type * list)
{
//do something
}
and its prototype in the header file is:
template <class Type> Delete(Type);
And I call the function as follows:
Delete(list_head);//list head is a pointer to a structure
I get an unresolved external:
int __cdecl Delete(struct V_FSM *)" (?Delete@@YAHPAUV_FSM@@@Z)
Whats wrong with this code?
1. unless it's a typo, your prototype doesn't match the implementation - the argument has to be a pointer.
2. template code must reside in the header file - so you probably want to replace the header declaration of the function with the actual definition. this is the usual reason for the unresolved external error.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|