static member of template class

Discussion in 'C++' started by Tino, Sep 30, 2003.

  1. Tino

    Tino Guest

    From Stroustrup( TC++PL, Appendix C.13 ) I thought that this was the
    way (and the only way perhaps) to have a common static member for all
    classes generated from a template. Though compiling this with MSVC++
    6, it compiles but errors on linking:

    unresolved external symbol "protected: static class structure *
    store_base::m_pST

    I know that MSVC++ 6.0 has shoddy support of template features...does
    this case fall in that class. I'd welcome any alternatives or
    workarounds.

    Regards,
    Ryan

    class structure{
    public:
    int i, j;
    structure(int i1,int j1) : i(i1), j(j1) {}
    };

    class store_base{
    protected:
    static structure* m_pST;
    public:
    static void set_structure( structure* pST ){ m_pST = pST; }
    };

    template<class T> class store : public store_base{
    private:
    T **buffer;
    public:
    store() : buffer( NULL ) {}
    bool Allocate();
    T operator()(int i1, int j1) const
    {
    if( i1 >= 0 && i1 < m_pST->i && j1 >= 0 && j1 < m_pST->j )
    return buffer[i1][j1];
    }
    void set(int i1, int j1, T tVal)
    {
    if( i1 >= 0 && i1 < m_pST->i && j1 >= 0 && j1 < m_pST->j )
    buffer[i1][j1] = tVal;
    }
    void print();
    };

    template<class T> bool store<T>::Allocate()
    {
    buffer = new T*[m_pST->i];
    if( buffer == NULL )
    {
    cout << "unable to allocate" << endl;
    return false;
    }
    for( int n=0; n<m_pST->i; n++ )
    {
    buffer[n] = new T[m_pST->j];
    if( buffer == NULL )
    {
    cout << "unable to allocate" << endl;
    return false;
    }
    for( int m=0; m<m_pST->j; m++ )
    buffer[n][m] = T((m_pST->i+m_pST->j)/2);
    }
    return true;
    }

    int main()
    {
    structure myst(3,4);
    store_base::set_structure( &myst );
    store<int> storeint;
    store<double> storedouble;
    storeint.Allocate();
    storedouble.Allocate();
    return 0;
    }
    Tino, Sep 30, 2003
    #1
    1. Advertising

  2. > From Stroustrup( TC++PL, Appendix C.13 ) I thought that this was the
    > way (and the only way perhaps) to have a common static member for all
    > classes generated from a template. Though compiling this with MSVC++
    > 6, it compiles but errors on linking:
    >
    > unresolved external symbol "protected: static class structure *
    > store_base::m_pST


    http://groups.google.ca/groups?q=pr...b.33578$&rnum=1


    Jonathan
    Jonathan Mcdougall, Oct 1, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon G Best
    Replies:
    2
    Views:
    418
    Simon G Best
    Sep 25, 2003
  2. Siemel Naran
    Replies:
    4
    Views:
    786
    Micah Cowan
    Jan 12, 2005
  3. christopher diggins
    Replies:
    16
    Views:
    731
    Pete Becker
    May 4, 2005
  4. Frederiek
    Replies:
    1
    Views:
    370
    Thomas Tutone
    Sep 14, 2006
  5. dolphin
    Replies:
    3
    Views:
    1,324
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page