Sign in to follow this  

Return an array

This topic is 4276 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 wrote a class a while back that handles creating and using a 2D array. Works like a charm. Latley, I've needed to write the data of the actual array to a file, so I wrote a little function in the class, returnArray, to, well, return a the array. returnArray takes the adress of a temporay array, then copys the array to thes temporary array.
template<class T> void array2D<T>::returnArray(T &destArray)
{
	for(int i = 0; i< (ARRAY_COLUMNS * ARRAY_ROW); i++)
		destArray[i] = Array[i];
	
}
seems simple enough. Array is of type T btw, aka, it's a templated class. So I then use it like thus:
//create the array to hold that map data.
	array2D<int> tempMapData;
	
	//3 X 3
	tempMapData.createArray(3,3);
	
	//map tiles
	/*
	0,1,2
	2,1,0
	1,1,1
	*/
	
	//set that up
	tempMapData(0,0) = 0;
	tempMapData(0,1) = 1;
	tempMapData(0,2) = 2;

	tempMapData(1,0) = 2;
	tempMapData(1,1) = 1;
	tempMapData(1,2) = 0;

	tempMapData(2,0) = 1;
	tempMapData(2,1) = 1;
	tempMapData(2,2) = 1;

	int *tempArray = new int [tempMapData.retnCol() * tempMapData.retnRow()];

	tempMapData.returnArray(tempArray);

	for(int i = 0; i<9;i++)
		mapWriter.write<int>(tempArray[i]);

Alas, all is not well in programming land. I get a lovely:
e:\programing\directx\nocturne engine\source\nocturne\cmap.cpp(117) : error C2664: 'array2D<T>::returnArray' : cannot convert parameter 1 from 'int *' to 'int &'
        with
        [
            T=int
        ]
soo...I've fiddled, and I'm sleepy, and I have no clue wtf i'm doing wrong. I feel kinda dumb, like I should know this, so please don't be too mean :P Thanks!

Share this post


Link to post
Share on other sites
*smacks head*

Yup, that works. The actual function doesn't actuall work (something is really messed somehwere) but ya, thanks!

Share this post


Link to post
Share on other sites

This topic is 4276 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this