Thread: bug for static functions in template class

  1. #1
    Unregistered
    Guest

    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;
    }

  2. #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

  3. #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

  4. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 10:17 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Operator overloading in template classes
    By moejams in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2003, 05:16 PM