Page 1 of 1

template question [solved]

Posted: Sat Aug 20, 2011 6:21 am
by short
Hey guys, I'm taking the time to become familiar with templates, and because of that I think it's fair to say I have atleast one question.

I have a template class MemoryPool<T>.

MemoryPool has a private member AbstractFactory<T> named _factory

In MemoryPool's constructor, I use _factory

Here's the code (I stripped out everything but the following):

MemoryPool.h

Code: Select all

template <typename T>
	class MemoryPool
	{
		typedef std::shared_ptr< MemoryItem<T> > MemoryItemSharedPointer;
	public:
		MemoryPool(const size_t itemsToInitNow, const size_t maxItems) : _maxItemsInMemoryPool(maxItems)
		{
			// initialize itemsToInitNowitems immediately
			for(size_t i = 0; i < itemsToInitNow; ++i) {
            _factory.CreateItem(); // this fails :( // just an example of how to trigger the failure
				_container.push_back(MemoryItemSharedPointer(new MemoryItem<T>(_factory.CreateItem()))); // this fails (and it is what I really want to do)
			}
		}

	private:
		// private data members
		AbstractFactory<T> _factory;
Elsewhere in the code, when I go to create a MemoryPool with a concrete type, with something such as

Code: Select all

MemoryPool<ParticleShape> mp;
I get the lovely error message:
System::Abstract<T>::CreateItem<void> : could not deduce template argument for T
My thoughts were that the _factory would be instantiated with the same type T that MemoryPool was instantiated with, but it says that it couldn't deduce the template type for _factory. I really just want _factory's type to be the same type that MemoryPool<T> is.

Thanks to anyone who has any clue :)

Re: template question

Posted: Sat Aug 20, 2011 3:03 pm
by THe Floating Brain
I have two possible answers:
A:
Did you comment out everything but the data members? Sometimes you get problems with using the actual template objects, rather than declaring them. In some of these cases the intellesence takes you to the decoration anyway.
B:
In my experience with boost I have found that it messes up templates, sadly if this is the case I have no solution.

Re: template question [solved]

Posted: Sun Aug 21, 2011 12:22 am
by short
Btw I'm an idiot, here was the AbstractFactory.h class I had

Code: Select all

#ifndef _ABSTRACTFACTORY_H_
#define _ABSTRACTFACTORY_H_
#include <cstring>
#include <memory>


namespace System
{	
	template <typename T>
	/// <summary>	Factory template. </summary>
	/// <remarks>	Factory for instantiating new instances of T. </remarks>
	class AbstractFactory
	{
	public:
		AbstractFactory(void){ }

		~AbstractFactory(){}
		
		// public member functions
		/// <summary>	Creates the item on the heap. Returns a shared_ptr to the item.</summary>
		/// <typeparam name="T">	Generic type parameter. </typeparam>
		/// <returns>	shared_ptr of type T.</returns>
		template <typename T>
		inline const std::shared_ptr<T> CreateItem()
		{
			return std::shared_ptr<T>(new T);
		}

	private:
		// Disable copy constructor and assignment operator
		AbstractFactory(const AbstractFactory& ref);
		AbstractFactory &operator=(const AbstractFactory&);
	};
}

#endif
The problem was the inner template parameter on the CreateItem() method, removing that seems obvious now.