Hai!

I tried writing a stack program using the concept of templates.
It worked well for the int,char and double.But when i tried to pass a complex class error occured.

Is it possibleto pass a class to the template class?
If so what is the syntax for doing it?

I have got the code attached with this mail please specify if any changes is to be made in it.


Urs,

PriyaJaiGanesh

what error is it showing as the code looks fine to me

what error is it showing as the code looks fine to me

It shows that "default constructor for the stack<complex> is not found"

I tried adding the complex header file instead of the one i defined and surprisingly it worked correctly.

But still i want to know y it didn't work for my code or wat was wrong in my code.

Thank u so much for taking effort to clear my doubt.

urs,
PriyaJaiGanesh

OIC!! the complex class is being initialised with no params when you use it like that - so you need to code a default constructor (one with no parameters). This should be standard practice but try adding:

/* INSIDE the complex class */

complex(void)
{
    real = imag = 0;
}

This means when you use complex as a type/cast it will set real and imag to zero until you do anything with it - which wont matter in your case. Try the code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.