C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-16-2001, 04:15 PM   #1
Unregistered
Guest
 
Posts: n/a
Question bug for static functions in template class

Here is myc ode, I could compile it, but could not link. Any body has any idea? Thanks.

===================
#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;
}
  Reply With Quote
Old 09-16-2001, 04:25 PM   #2
Registered User
 
Esss's Avatar
 
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;
* Where is your constructor?
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   Reply With Quote
Old 09-16-2001, 05:54 PM   #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   Reply With Quote
Old 09-16-2001, 06:38 PM   #4
Toaster
 
Zach L.'s Avatar
 
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:40 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22