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.

8 Years
Discussion Span
Last Post by William Hemsworth
// delete any previous data
delete [] inStart;

This is a very bad idea, and no data needs to be deleted, just overwrite it.

inLast = inStart = new inType[size]; // = new T[size]?

Both inLast and inStart are not pointers and therefore you can not assign a pointer to them, they are references. Make sure you know the difference between the two.

I hope this helps you understand your problem a little better.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.