• Advertisement
Sign in to follow this  

Problem with typedefs and templates in C++

This topic is 4478 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have this code:
template <class T> 
class ParameterContainer
{
	public:
	ParameterContainer();
	T * parameterarray;
	int sizeparameterarray;
};

ParameterContainer::ParameterContainer()
{
	sizeparameterarray = 0;
	parameterarray = 0;
}

typedef bool (*pointertoFunc) (ParameterContainer);
Now, what I want is to make ParameterContainer a class template and let pointertoFunc be a type that accepts a pointer to a function that returns a bool and accepts a ParameterContainer as a parameter. (I will do a special implementation of <char> ParameterContainers, btw.) But Visual C++ complains that I need to use a parameter list to use class templates. How can I fix this or am I doing this in an utterly dumb way? -MentalMan

Share this post


Link to post
Share on other sites
Advertisement
A function cannot accept ParameterContainer as a type. ParameterContainer is not a type. It is a template. ParameterContainer<Something> is a type.

Share this post


Link to post
Share on other sites
You are very close. When you define your contructor (or any other function) you need to specify that it its templated. Secondly, when you are trying to typedef your function pointer you need to provide a concrete class for the template.


template <class T>
class ParameterContainer
{
public:
ParameterContainer();

T * parameterarray;
int sizeparameterarray;
};

template <class T>
ParameterContainer<T>::ParameterContainer()
{
sizeparameterarray = 0;
parameterarray = 0;
}

typedef bool (*pointertoFunc) (ParameterContainer<int>);



Alan

Share this post


Link to post
Share on other sites
You could also put the typedef in the namespace of the class, like this:
template <class T> 
class ParameterContainer
{
public:
typedef bool (*pointertoFunc)(ParameterContainer<T>);
...
};

// use typedef
ParameterContainer<int>::pointertoFunc pFoo;





[Edited by - spoulson on January 19, 2006 3:59:38 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement