template problem

Hi

I've template class that may get string and integer,
if I'm using it with int it's fine but if I use it with string I get an error,
because string need to be "" and not NULL.
what can I do?

this is the code:
1
2
3
4
5
template <class K>
Item<K>::Item()
{
	key=NULL;
}
Last edited on
Do it like so:
1
2
3
4
5
template <class K>
Item<K>::Item()
{
	key=K();
}
thank you, but It's good to string but error for int
Last edited on
Nope, it's good for both. Did you compile it?

If you get an error, get a more recent version of your compiler.
my mistake
It's fine
thank you
It's better to initialize key in the constructor initialization list.
1
2
3
4
5
template <class K>
Item<K>::Item()
:	key()
{
}

Last edited on
Topic archived. No new replies allowed.