Template class Issue

Started by
8 comments, last by alchemar 19 years, 6 months ago
Hello, I am making a template 2D array class Called TArray2D. I have an Initialize function that looks like this.

bool Initialize (unsigned int Width, unsigned int Height, const T& value);

what this does is make a std::vector of size Width * Height and fills this vector with value. This works fine. But I also have a function called GetSubArray and it looks like this.

bool GetSubArray (unsigned int X, unsigned int Y, unsigned int Width, unsigned int Height, TArray2D<T> &ArrayOut, unsigned int &WidthOut, unsigned int &HeightOut);

X and Y are the upper left cell to get this subarray from, Width and Height are how many cells to get in each direction. as you can see I pass a TArray2D class Referance to this function which is the template class itself. Is there any way that I can Initialize the class I pass in to the GetSubArray function within the function? Right now I use the very first cell to do the Initializeation with. but I don't like to do that. I would rather use some blank type T for this.
Advertisement
So what you want to do is reinitialise the TArray2D object to a smaller array and do this within the GetSubArray function?

I don't see why there is a problem here. Simply call Intialise again with the new width, height and value and make sure that sure that either Initialise first tests for an empty std::vector or shrinks/grows it to the appropriate size defined by width*height.

The only case where you will may some issues is when Width and Height are stored by reference in your TArray2D class.

Timkin
My problem has been getting the type T for the initialization in the class since I don't know what T is. I guess I don't know how to make a variable of type T.
Just use T as the variable type:
template <typename T>T myfunc(T in) {  T result;  result.mungeWith(in);  return result;}


Quote:My problem has been getting the type T for the initialization in the class since I don't know what T is. I guess I don't know how to make a variable of type T.

You mean like this?
ArrayOut.Initialize(WidthOut,WidthHeight,T());
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
I should have thought of that joanusdmentia thank you thats what I needed.
Maybe one of you guys can help me with this. here is the function.

	bool GetSubArray(unsigned int X, unsigned int Y, unsigned int Width, unsigned int Height, TArray2D<T> &ArrayOut, unsigned int &WidthOut, unsigned int &HeightOut)	{		// check to see if X or Y is to large for array		if ((X > mDimX)||(Y > mDimY))		{			WidthOut = 0;			HeightOut = 0;			return true;		}		// check to see if we over run the array and set Height and Width		if (((X+Width)-1) >= mDimX)	// to many wide		{			Width -= (Width - mDimX);		}		else		{			Width = (X + Width);		}		if (((Y+Height)-1) >= mDimY)	// to many high		{			Height -= (Height - mDimY);		}		else		{			Height = (Y + Height);		}		// set the out Height and Width		WidthOut = (Width - X);		HeightOut = (Height - Y);		// clear the array passed in		ArrayOut.Destroy();		ArrayOut.Initialize(WidthOut, HeightOut, T());		// loop through and fill subarray with array		unsigned int IndexX = 0;		unsigned int IndexY = 0;		for (unsigned int iy = Y; iy < Height; ++iy)		{			for (unsigned int ix = X; ix < Width; ++ix)			{				ArrayOut.SetValue(IndexX, IndexY, GetValue(ix,iy));				++IndexX;			}			IndexX = 0;			++IndexY;		}		return false;	}


my problem is that if I ask for a square subarray I get the correct subarray back. if I ask for a subarray that is wider then it is High I get the correct array back. but if I ask for an subarray that is higher then it is wide I get strange numbers.

From an array of
0000000000
1111111111
2222222222
3333333333
4444444444
5555555555
6666666666
7777777777
8888888888
9999999999

I get
0444
1555
2666
3333
4444
5555
6666

just a clearification i am asking for an array from 0,0 with a width of 4 and a height of 7

I have been knocking my head on the wall for hours. I would thank you for any help.

[Edited by - alchemar on September 29, 2004 9:23:58 AM]
Update -

the number of lines that have the wrong numbers are Width - height. so if I ask for "0,0,6,7,array" the very fist line would have bad numbers.

I get
066666
111111
222222
333333
444444
555555
666666

If I ask for "0,0,5,7,array" then the first 2 lines have bad numbers.

I get
055555
166666
222222
333333
444444
555555
666666

If I ask for "0,0,7,7,array" everything is right.
What does your GetValue() function look like?
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
LOL, i did finally find the problem and yes it was in both GetValue and SetValue instead of (Y*mDimX) + X I had (X*mDimX) + Y

Thanks for all your help

This topic is closed to new replies.

Advertisement