Skip to main content
gamedev.net
🔒 Locked

problem with template classes

Started by BrianH Oct 10, 2000 at 1:00 AM 5 replies 800+ views
Original Post
BrianH
BrianH
Hello, I am having a problem with a template class I created. The problem I am having concerns how to use it. here is how I have my files set up... --------------- chash.h I define the template class here... --------------- chash.cpp The implementation of the template class is here... --------------- now I want to use this class elsewhere in other files, this is where I am having problems. I have a manager type class that contains an instance of the template hash class, like this --------------- manager.h #include "chash.h" chash *framehash; --------------- manager.cpp //in the constructor framehash = new chash; --------------- Now here comes the problem, everything compiles ok but I get linker errors saying basically that manager.obj cant find the chash functions to link to. I can solve this by explicitly instantiating a chash inside chash.cpp... which becomes --------------- chash.cpp template class chash; //the rest.. --------------- I cant put the explicit instantiation anywhere else or I get compile errors. This just does not seem right to me. chash.cpp should not be ''aware'' of all the classes that will use it - that seems to defeat the purpose. All the examples of using template classes in every book I have put all the code into one file. I am just unsure how to do it and span it to many files. Hope you can understand my problem, Maybe if anyone has any example of using a template class in a practical - multifile - project. thanks, BrianH
milo
milo
Put all template code in the header file. Most (or all) compilers will not allow your template to be split into multiple files.

Mike Roberts
aka milo
mlbobs@telocity.com
Atavist
Atavist
What's with that !? STL gets to have its stupid header files seperate. It just seems to me that there must be a way to have our template implementation seperate.

Okay, it seem that I've lied again Even microsoft is forced to place their implementation inside the class definition.

I guess Ill shut up and fall inline...

Edited by - Atavist on October 10, 2000 8:10:46 PM
Just because the church was wrong doesn't mean Galileo wasn't a heretic.It just means he was a heretic who was right.
Jaxks
Jaxks
Well if you write functions to header they are forced to be inline. You can put function to external file with this trick.

        
//Array.h

template <typename T> class Array
{
private:
T * data;
public:
Array();//Default constructor

};

//Array cpp.

template <typename T>
Array<T>::Array()
:data=0
{
//do something

}

As you might see I made constructor as template function.
This was "cut" from Ivor Horton´s Beginning C++.


Edited by - jaxks on October 11, 2000 9:54:04 AM
Atavist
Atavist
Sorry Jaxks, but you cannot do that. If you try to have a third file (for example main.cpp) include that header file then your project will not link.

Either Ivor Horton´s doesn''t know what hes talking about, or you didn''t quote correctly from the book.

Just because the church was wrong doesn't mean Galileo wasn't a heritic.
It just means he was a heritic who was right.
Just because the church was wrong doesn't mean Galileo wasn't a heretic.It just means he was a heretic who was right.
JD
JD
Yup, under vc++ it won''t link. The implementation has to be in the header file same if you want your member functions to be inline. Also the data=0 has to be changed to data(0) for it to compile. It will compile data=0 if it''s put into cpp file but then it won''t link.
Jaxks
Jaxks

Sorry!


I haven´t read my template chapter yet. I see the topic and posted answer from that I found.
Thanks anyway now I´m not confused when I read templates.

data=0 was typing error.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.