Jump to content
  • Advertisement
Sign in to follow this  
alchemar

Template class Issue

This topic is 5408 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

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Just use T as the variable type:

template <typename T>
T myfunc(T in) {
T result;
result.mungeWith(in);
return result;
}


Share this post


Link to post
Share on other sites
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());

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!