template parameters in constructor
I'm trying to add parameters in constructor of my singleton. My code:
Code:
template <typename T>
class Singleton
{
public:
...
template<typename U, typename X>
static T* getInstancePtr();
...
protected:
Singleton();
template<typename U, typename X>
Singleton( U, X );
virtual ~Singleton();
private:
Singleton( const Singleton<T>& instance );
T& operator = ( const Singleton<T>& instance );
static T* _object;
};
This is my implementation in my "Singleton.inl" file:
Code:
template <typename T>
template <typename U, typename X>
inline T* Singleton::getInstancePtr()
{
if( _object == NULL )
_object = new T( U, X );
return _object;
}
template <typename T>
template <typename U, typename X>
inline Singleton::Singleton( U, X )
{
}
My compiler complains about the existing declaration:
Code:
error C2244: 'util::Singleton<T>::getInstancePtr' : unable to match function definition to an existing declaration
1> definition
1> 'T *util::Singleton::getInstancePtr(void)'
1> existing declarations
1> 'T *util::Singleton<T>::getInstancePtr(void)'
...
error C2244: 'util::Singleton<T>::{ctor}' : unable to match function definition to an existing declaration
1> definition
1> 'util::Singleton::Singleton(void)'
1> existing declarations
1> 'util::Singleton<T>::Singleton(const util::Singleton<T> &)'
1> 'util::Singleton<T>::Singleton(U,X)'
1> 'util::Singleton<T>::Singleton(void)'
Why this problem occurs? Can you help?
Thanks for replies
Re: template parameters in constructor
First, wouldn't it make more sense to just use a Singleton<T<U,V>> rather than jumping through these hoops? Trying to instantiate a singleton with parameters is a pain anyway, it goes against the pattern.
Second,
Code:
template <typename T>
template <typename U, typename X>
inline Singleton<T>::Singleton( U, X )
{
}
I'm fairly sure that's what you need.
Re: template parameters in constructor
Man, you're right. The solution is always there.
Thanks for your reply