   Template confusion
  Hi there!First time posting in "General Programming" ;)I have a class ResourceSlot that should do the following:Store the pointer to data (mesh, texture, shader, anything I´d like to have in the future), store the ID, store the filepath the data came from, and store by how many objects this data is used to be able to clean up unused data properly.this class´s header looks like this:template <class T>class ResourceSlot{public:ResourceSlot(void);ResourceSlot(int inID);ResourceSlot(T* inData, LPCTSTR inString, int inID);~ResourceSlot(void);voidSetDeletable(bool d);voidSetData(T* inData, LPCTSTR inString);boolRelease();inline T*GetData(){return this->m_pData;}inline LPCTSTRGetFileString(){return this->m_FileString;}inline boolIsDeletable(){return this->m_bDeletable;}inline voidIncrRefCount(){this->m_RefCount++;}inline voidDecrRefCount(){this->m_RefCount--;}private:T*m_pData;intm_ID;intm_RefCount;LPCTSTRm_FileString;boolm_bDeletable;};this compiles fine and every function is fleshed out in an according .cpp file.Now when I go on and try to build a std::vector using the ResourceSlot class, it still compiles fine, but the linker slaps my face with loads of unresolved externals (missing constructor for ResourceSlot<Mesh>(void) for example and more).For information purpose, this is an example of a vector I would like to use:std::vector<ResourceSlot<Mesh> > meshVec;Just for testing purposes I implemented a ResourceSlot<Mesh>(void) constructor additionally to the ResourceSlot<T>(void) one. The unresolved externals concerning this one disappeared.I thought that was what templates were there for?Am I missing something? Will I have to code all the constructors, functions and so on for every class I want to use ResourceSlot with? Why should I bother using templates then? There must be a better way for that, I guess I´m just too blind to see right now, so I´m asking here for your help.Thx for reading and for any suggestions.   
Quote:this compiles fine and every function is fleshed out in an according .cpp file.until more compilers support the export keyword, you will need to place the implementation in the .h file for templates...   ;  Unless your compiler supports the export keyword, and I'm willing to bet that yours doesn't, then you can't put the definition of a template in a separate source file without explicit instantiation for specific types. Without explicit instantiation, the complete definition of the template needs to be available at point of instantiation, which means, in effect, that the definition needs to go into the header. (Or an inline file of some sort, etc.)For more details see these articles: "Export" Restrictions, Part 1 and "Export" Restrictions, Part 2.   ;Quote:Original post by rip-offQuote:this compiles fine and every function is fleshed out in an according .cpp file.until more compilkers support the export keyword, you will need to pace the implementation in the .h file for templates...Indeed.  In the mean time, you can include the .cpp file at the end of your header rather than actually move all your existing code.  Just be sure to remove any using declarations and macros and such that you don't want leaking into the rest of your source files.CM   ;  gah! This is ugly.... well.... guess I´ll have to live with that. Thx for the damn quick response ;) Works now.   ;  lol, you´re way too fast for me :) Thx again.I´ve already triedinclude "ResourceSlot.cpp"because this wouldn´t be that messy. But I get an error saying that the template function has already been defined for every function in the .cpp then. How do I fix that?[edit]fixed code[/edit]   ;Quote:Original post by matches81lol, you´re way too fast for me :) Thx again.I´ve already triedinclude "ResourceSlot.cpp"because this wouldn´t be that messy. But I get an error saying that the template function has already been defined for every function in the .cpp then. How do I fix that?[edit]fixed code[/edit]Is the #include inside your inclusion guards?  It needs to be parsed just once as well.#ifndef HEADER_H_#define HEADER_H_template<typename T> class Whatever {...};#include "source.cpp"#endif //HEADER_H_;  I don´t use those inclusion guards, I thought '#pragma once' would suffice to make sure the whole header is only parsed once.But perhaps I´m wrong there?I just put the #include at the end of my header.   ;  okay, I´ve tried using inclusion guards, too. No success, so I guess '#pragma once' is not the one to blame. By the way, I´m using MS Visual C++ 7 (2003)The whole bunch now looks like this://HEADER#pragma once#include <d3dx9.h> //LPCTSTRnamespace PUKE{template <class T>class ResourceSlot{public:ResourceSlot(void);...};};#include "ResourceSlot.cpp"//------------------------------------------------------------------------------//------------------------------------------------------------------------------//SOURCE#include "ResourceSlot.h"template <class T>PUKE::ResourceSlot<T>::ResourceSlot(void){this->m_ID = -1;this->m_RefCount = -1;this->m_bDeletable = true;this->m_FileString = "";this->m_pData = NULL;};  Are you still compiling ResourceSlot.cpp as a source file?   