Adapting an initializer for arrays

Hi,
I am trying to adapt my initializer template to arrays.

This is my working template:
1
2
3
4
5
template <class T>
void fnInitialize(T& v1)
{
    v1 = T();
}


This is my first attempt at converting it to apply to arrays.
1
2
3
4
5
6
template <class T>
void fnArrayInitialize(T arr[], int size)
{
	for(j=0; j<size; j++)
		arr[]();
}


Happily accepting suggestions
ERandall
closed account (zb0S216C)
arr[]();

I have no idea what it's supposed to do, but is doesn't initialise anything.

By the time the array is passed to the function, the array would have been fully initialised. The function actually assigns new values to the array. So effectively, you're just replacing the existing values with new ones.

Maybe you meant something like this:

1
2
for(int I(0); I < Size; ++I)
    Array[I] = T();

Wazzak
Last edited on
thanks, this is stopping the link errors. (Now to get my homemade functions header in the #include's directory in VS2010.)

ERandall
This sucks. My array initializer just stopped being legal.

Template:

1
2
3
4
5
template <class T>
void fnInitialize(T arr[], int size)
{
	for(int j=0; j<size; j++)
		arr[j]=T();


The calls to the template:

1
2
3
4
5
6
7
8
9
10
11
12
        string arrNames[NUMB_STUDENTS];
        fnInitialize(arrNames[NUMB_STUDENTS]);

	int arrScores[NUMB_STUDENTS][NUMB_TESTS];
        fnInitialize(arrScores[NUMB_STUDENTS][NUMB_TESTS]);
	
	double arrAverageScore[NUMB_STUDENTS];
        fnInitialize(arrAverageScore[NUMB_STUDENTS]);

	LtrGrade arrGrade[NUMB_STUDENTS];
        fnInitialize(arrGrade[NUMB_STUDENTS]);


Now what have I done?
You confused [] with a comma. You're passing the element beyond the last one when you should be passing a pointer to the first and the array size.
Last edited on
Thank you, that helped all but the multi-dim array. I keep 'hosing' my stack. "More cowbell!!!" ("research")
Topic archived. No new replies allowed.