Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Any Help - Urgent - Class Template

Status
Not open for further replies.

sd110404

Programmer
Nov 3, 2004
63
0
0
US
Hi,
I am new in implementing Templates.

My .h file.
class AnimalControl
{
public:
DogCatcher* getDogCatcherRef();

void disturbance();
void incarcerate(Animal* temp_ptr);

AnimalControl(); // constructor
~AnimalControl(); // destructor

private:
int startpos; //start point of incarcerate
int insertpos; //first free slot
DogCatcher* dogcthr;
Animal* ptr_animal[10];

};

Inside my .cpp file
AnimalControl::AnimalControl()
{
cout << "AnimalControl constructor: " << endl;
startpos=insertpos=-1;
dogcthr=new DogCatcher(this);
cout << "\n\n";
} // end AnimalControl constructor

// destructor
AnimalControl::~AnimalControl()
{
for(int i=startpos;i<=insertpos;i++)
{
cout << "AnimalControl destructor: "<< endl;
ptr_animal->~Animal;
}
cout << "\n\n";
} // end AnimalControl destructor
//incarcerate method
void AnimalControl::incarcerate(Animal *temp_ptr)
{
if(startpos==0&&insertpos==9)
{
cout<<"\nNo free slot..Memory full";
}
else //finding first available free slot
{
if(startpos==-1)
startpos=insertpos=0;
else if(insertpos==9)
insertpos=0;
else
insertpos++;
ptr_animal[insertpos]=temp_ptr;
}
}// end incarcerate method

void AnimalControl::disturbance()
{
for(int i=startpos;i<=insertpos;i++)
{
(ptr_animal)->vocalize();
}
}


I need to Templatize the AnimalControl class, then rename the AnimalControl class as ControlFacility. Convert it to be template class. And at the end all uses of class Animal are replaced with the template parameter T.

I know to use template with functions and how to declare. But not with class, and also how to define.
Can anyone please help me how to rename and templatize.
Any help will be very helpful.
Thanks,
Tina.
 
If you convert it to a template, you must move all the code into the .h file.

Code:
template <class T>
class ControlFacility
{
public:
    DogCatcher* getDogCatcherRef();

    void disturbance();
    void incarcerate(T* temp_ptr);
    
    ControlFacility(); // constructor
   ~ControlFacility(); // destructor
   
private:
    int startpos;  //start point of incarcerate
    int insertpos;  //first free slot
    DogCatcher* dogcthr; 
    T* ptr_animal[10];
};
 
Hi cpjust,

Thanks for helping me at the right time.
You mentioned if I convert to template I need to move all the code to .h file.
Can you please explain that. I mean which code i need to move?
Did u mean .cpp??

And do i have to keep AnimalControl class?
 
Having a [tt].cpp[/tt] file at all for a template class is meaningless... unless your compiler supports the [tt]export[/tt] keyword; it probably doesn't.

Put everything into the [tt].h[/tt] file.
 
All the code in the .cpp file that belongs to your template class should move into the header file.
Ex.
Code:
template <class T>
class ControlFacility
{
public:
    DogCatcher* getDogCatcherRef()
    {
        // Put your code here.
    }

    void disturbance()
    {
        // Put your code here.
    }

    void incarcerate(T* temp_ptr)
    {
        // Put your code here.
    }
    
    ControlFacility() // constructor
    {
        // Put your code here.
    }

   ~ControlFacility() // destructor
    {
        // Put your code here.
    }

private:
    int startpos;  //start point of incarcerate
    int insertpos;  //first free slot
    DogCatcher* dogcthr; 
    T* ptr_animal[10];
};
If you don't have the function definitions in the same file as the declarations you'll probably still be able to compile, but it won't work right when you run the program.
I'm not very good at explaining why templates work that way (maybe someone else can help out), but I know it has something to do with the way templates are linked into your program...
 
I'm not very good at explaining why templates work that way
Neither am I... and I wrote an FAQ on it! ;-) Read at your own risk...

faq208-2798
 
thanks a lot, i put everything in my .h file and it works great.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top