Skip to main content
gamedev.net
🔒 Locked

Problem with Inlining

Started by waZim 1 reply 550+ views
Original Post
waZim
waZim
Guys, I have an issue with Inline Functions, consider the following header file for stack. //////////////////////////////////////////////////////////////////////////////////// // Stack.h #ifndef __STACK_H_INCLUDED__ #define __STACK_H_INCLUDED__ namespace Engine { template <class _Type> class CStack { public: __forceinline CStack(void); virtual ~CStack(void); private: // Data Needed for Stack }; } // This is where i can put the Destructor Body which works //template <class _Type> //Engine::CStack<_Type>::~CStack( void ) //{ //} #include "Stack.inl" #endif // __STACK_H_INCLUDED__ /////////////////////////////////////////////////////////////////////////////// // Stack.inl namespace Engine { template <class _Type> __forceinline CStack<_Type>::CStack(void) { // Allocate Memory } } /////////////////////////////////////////////////////////////////////////////// // Stack.cpp #include "Stack.h" namespace Engine { //-----------------------------------------------------------------------// template <class _Type> CStack<_Type>::~CStack(void) { // do the deletion here } //-----------------------------------------------------------------------// } //////////////////////////////////////////////////////////////////////////////////// Now the Problem, If i define the Destructor in the header file after the Class Difinition, i get a successfull compile, But if the Body of the Destructor is in the Stack.cpp file i get a Linker Error LNK2019. Now if i remove the Virtual Qualifier from the ~CStack(void),,, and define the function any where in header or inline or cpp, i get a successfull compile again,, what is going on? i can't understand all this . Please Note the Namespace "Engine" Used and also the __forcinline instead of inline, but they essentially bahave the same in my case. any kind of help would be greatly appriciated. .. waZim
rip-off
rip-off
Template functions need to be visible where used*, like inline functions. Keep them in the header file, or in a separate file included by the header (again, like your inline function).


* Not entirely true - currently there are two ways of avoiding this. One is to use explicit instantiation and the other is to use a compiler that supports the "export" keyword. Neither are really practical for most template classes.
waZim
waZim
Thanks for quick reply, atleast i have stoped wasting time on trying to put the code difinitions in CPP File, :)

i knew it works in header file or inline file but it looks a little ugly i thought there might be another way of putting it inside cpp..

Topic Locked

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

Sign in to reply to this topic.