template classes with static members 
Author Message
 template classes with static members

Hello!
I already posted this question to microsoft.public.vc.language but no
one answered me. Maybe here?

I want to create some template class with static member variables. Does
anybody know how to do it and avoid the "LNK2001 Unresolved external"?
The problem is that I should explicitly specify which cpp file will
contain instantiated static members. Is there a way to make a compiler
to resolve this problem without me?
Best Regards, Sergey Surkov

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Fri, 10 May 2002 03:00:00 GMT  
 template classes with static members

hi! I had the same problem (not om "template" though, but may be it will
trigger some idea) with  my singleton classes...this is how i resolved
it...code directly taken from the source.

/* x2y.h file */

class CValidTrans: public CX2YTrans
{
private:
 static CX2YTrans* m_pX2Y;

protected:
 CValidTrans();
 virtual ~CValidTrans();

public:
 INT IsValid(){return SUCCESS_CODE;} /*overriden virtual function*/
 void GetMessage(string& strMes){strMes = "Valid Mapping";}/*overriden
virtual function*/

 static CX2YTrans* GetInstance();
 static void DeleteInstance();

Quote:
};

/* x2y.cpp file */
CX2YTrans* CValidTrans::m_pX2Y = 0;

CValidTrans::CValidTrans(){}
CValidTrans::~CValidTrans(){}

CX2YTrans* CValidTrans::GetInstance()
{
 if(m_pX2Y == NULL)
  m_pX2Y = new CValidTrans;
 return m_pX2Y;

Quote:
}



Quote:
> Hello!
> I already posted this question to microsoft.public.vc.language but no
> one answered me. Maybe here?

> I want to create some template class with static member variables. Does
> anybody know how to do it and avoid the "LNK2001 Unresolved external"?
> The problem is that I should explicitly specify which cpp file will
> contain instantiated static members. Is there a way to make a compiler
> to resolve this problem without me?
> Best Regards, Sergey Surkov

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Sat, 11 May 2002 03:00:00 GMT  
 template classes with static members

Quote:

> I want to create some template class with static member variables. Does
> anybody know how to do it and avoid the "LNK2001 Unresolved external"?

template <class T>
struct foo
{ static int x; }

template <class T>
int foo<T>::x;

--



Sat, 11 May 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. template classes with static members

2. template classes with static members

3. static data member of template class

4. Initialization of static data member of template class

5. Static member definition of template class fails

6. Static member definition of template class fails

7. Static member variables of template class

8. static template class member variable in DLL

9. static members in template class across DLL's

10. template class with a template member function

11. Template member functions of template classes

12. calling template member function in template class

 

 
Powered by phpBB® Forum Software