• 0

Template Methods help


Question

Hi all,

I have a problem with template methods, what I want to do is pass in a struct type as a template variable but I don't know the correct way to do it.

Here is the method:

template<typename T>
void SetSize(unsigned int size, string inClass, T &inStart, T &inLast, T &inEnd, T inType) {

	// delete any previous data
	delete [] inStart;

	// allocate new particles
	if (inClass == "Particle")
	{
		inLast = inStart = new inType[size];
	}

	// set end
	inEnd = inStart+size;
}

As you can see I want the inType to be able to be any struct that I pass in as an argument.

but when I try:

SetSize(100000, "Particle", start, last, end, Particle);

Particle is a struct but it says:

"Illegal use as this as an expression"

Please help,

thanks in advance.

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

My 2 cents : try specifying the type parameter : SetSize<typeofyourstruct>(parameters...) and see if the compiler says anything different.

Also, are you sure "start", "last", "end" and "Particle" are of the same type, at compile-time (no polymorphism!) ? Otherwise, the compiler doesn't know what type to use to instantiate the method.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.