-
user defined template
hello, i am trying to build a Queue, with the use of templates, so that it is possible to create 'int', 'string' queues and so on...
i have been given a class definition for my template queue (TQueue), which includes this:
void add(Node<T> *n);
i have created a my queue like this:
TQueue<int> myQueue(); //works fine!
my problem is when i am to add Nodes, i do this:
int number=10;
Node<int>* myIntNode=new Node<int>(number);
myQueue.add(myIntNode); //the compiler doesn't accept this
regarding my last operation, i get the following error msg:
"Structure required on left side of . or .*"
what am i doing wrong?
anyone? :)
-
I can't see anything wrong with your add function from the info that you've given, but you'll need to change -
TQueue<int> myQueue();
to
TQueue<int> myQueue;
C++ has difficulty differentiating between construction and function declaration.
-
if i don't use the brackets i get the following linker error:
[Linker Error] Unresolved external 'TQueue<int>::~TQueue<int>()' referenced from C:\PROGRAMMING\C++\EXERCISE V\DRIVER.OBJ
my implementation looks like this:
template<class T>TQueue<T>::TQueue()
{
head=tail=current=NULL;
}
template<class T>TQueue<T>::~TQueue()
{
//not done yet!
}
template<class T> void TQueue<T>::add(Node<T> *n)
{
if(head==NULL)
{
head=tail=current=n;
head->link(NULL);
}
else
{
current=tail;
current->link(n);
tail=current->link();
}
}
plus some more, but this is out of context regarding my problem.
see anything wrong. i normally get linker errors if i forget to declare constructors, but i cannot see anything wrong with my constructor (?).
thanks in advance
-
Looks Ok, is the implemenation in the header file?
-
no, my implementation is in a cpp file ( i have a separate header file).
i don't seem to get this!
xagiber
btw, i use borland builder5
-
You cannot have templates split into source and header. You will ave to put it all into the header.
-
THAT was my problem! thanks a lot!
-
It is possible to split template declarations and implementations.
You can do this....
Templates.h
// your template declarations go here
// then one of those rarae times you will need to do this...
#include "Templates.cpp"
Templates.cpp
//Your templates implementations go here....
2 different files. Yet the compiler sees them as one!
-
Hi stoned-coder
Thanks for that - I was getting a linker error - undefined
sac<int>::sac(int) from main() // sac being the template
Didn't realize that template declaration and implementation had to be together (using Dev-C++)
Thought they'd work just like ordinary classes
But...
What if you have say two separate files eg main.cpp and other.cpp and they both use the same template class 'tclass' ?
Does #including tclass.h in both cause a problem ?
I have heard that MVC++ also requires that dec and imp have to be together - anyone know how Mickeysoft suggest structuring code around the problem ?
-
>Does #including tclass.h in both cause a problem ?
No you should be alright with templates. Another alternative is to #inlude the cpp at the bottom of your header file. Then you can just include the header file. However, if you want other people to be able to use your class you'll still have to include all the files and can't hide the source in a library, so the most common method is to write the implementation in the header file.
>anyone know how Mickeysoft suggest structuring code around the problem ?
The same way as all the other compilers that don't support the export keyword.
-
Thanks Sorensen
I found a similar answer in a serach
//tclass.h
#ifndef tclass_h
#define tclass_hh
template <class blah...
#include tclass.cpp
#endif
//tclass.cpp
#ifndef tclass_cpp
#define tclass_cpp
// imp code
#endif