CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2005
    Posts
    173

    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

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  3. #3
    Join Date
    Oct 2005
    Posts
    173

    Re: template parameters in constructor

    Man, you're right. The solution is always there.
    Thanks for your reply

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured