![]() |
| | #1 |
| Guest
Posts: n/a
| =================== #include <iostream.h> template < class T> class Base { public: Base(); static int getNum(); static void setNum(int); private: static int m_num; }; static int m_num = 0; template <class T> int Base<T>::getNum() { return m_num; } template <class T> void Base<T>::setNum(int num) { m_num=num; } void main() { Base<int> A; A.setNum(10); Base<double> B; cout<< "Base<T>::m_mum is " << B.getNum() <<endl; } |
|
| | #2 |
| Registered User Join Date: Aug 2001
Posts: 133
| Two things: * Your declaration of the static member variable is incorrect: Code: template <class T> int Base<T>::m_num = 0; Code: template <class T>
Base<T>::Base()
{
}
__________________ Ess Like a rat in a maze who says, "Watch me choose my own direction" Are you under the illusion The path is winding your way? - Rush |
| Esss is offline | |
| | #3 |
| Registered User Join Date: Sep 2001
Posts: 156
| Templates are sneaky fellows. If they are not defined in the .h file you will have problems with the template definition. Template definitions (storage allocation) doesn't occur until you create an object. If you have your template implementation is in a .cpp it will compile with the <T> type you're using but create only the declaration of the type and move on. Again templates only allocate storage when instantiated. If the template definition is in the .h file the storage will be allocated when instantiated. But there will be no storage allocated if its in a .cpp because its already compiled and it created only the declaration prior to the instance. If you have Bruce Eckel's Thinking in C++ look at page 400 for a better explanation. thanks dan |
| Dang is offline | |
| | #4 |
| Toaster Join Date: Aug 2001
Posts: 2,686
| Actually I think that it all is in one file. I think Esss got it right. It looks like linker saw the declaration for the constructor in the class declaration, but it could not find the definition for the constructor. Always make sure to define what you declare (at least with functions; with variables it won't offend the compiler not to define them), otherwise, the linker will look for the definition of that function, and will be unsuccessful, so it will generate an error.
__________________ The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop. |
| Zach L. is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Getting an error with OpenGL: collect2: ld returned 1 exit status | Lorgon Jortle | C++ Programming | 6 | 05-08-2009 08:18 PM |
| Function template has already been defined | Elysia | C++ Programming | 19 | 04-14-2009 10:17 AM |
| Message class ** Need help befor 12am tonight** | TransformedBG | C++ Programming | 1 | 11-29-2006 11:03 PM |
| Class Template Trouble | pliang | C++ Programming | 4 | 04-21-2005 04:15 AM |
| Operator overloading in template classes | moejams | C++ Programming | 5 | 07-21-2003 05:16 PM |