template question [solved]

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

template question [solved]

Post 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 :)
Last edited by short on Sun Aug 21, 2011 12:20 am, edited 1 time in total.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
THe Floating Brain
Chaos Rift Junior
Chaos Rift Junior
Posts: 284
Joined: Tue Dec 28, 2010 7:22 pm
Current Project: RTS possible Third Person shooter engine.
Favorite Gaming Platforms: PC, Wii, Xbox 360, GAME CUBE!!!!!!!!!!!!!!!!!!!!!!
Programming Language of Choice: C/C++, Python 3, C#
Location: U.S

Re: template question

Post 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.
"Why did we say we were going to say we were going to change the world tomorrow yesterday? Maybe you can." - Myself

ImageImage
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: template question [solved]

Post 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.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
Post Reply