Template class

Hi,

I have a template class called Array that comprises a type and a nontype parameter.

I have the following code for my constructor.

1
2
3
4
5
6
7
8
9
10
11

template <typename T, int arraySize> 
Array<T,arraySize>::Array()
    : size( arraySize )
{
    ptr[arraySize]; // create space for pointer-based array
	
    cout << ptr[0] << endl;

}


I have been told not to use the new operator because it is unnecessary because the elements will be initialized by the default constructor of whatever class they belong to. However, every time I try and access the first element of the array to print it out or change it I get an unhandled exception.

Any ideas?

Thanks,

Sean
Since you're not going to use a dynamic array, use a static array (it's not like you have more options).
Your array class should be
1
2
3
4
template<typename T, int N>
class Array{
   T arr[N];
};
Sorry. Not sure I get it. I'm new to C++. I've got more of a Java background. Here's my class definition.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
template <class T, int size> class Array
{
   friend ostream &operator<<( ostream &, const Array & );
   friend istream &operator>>( istream &, Array & );
public:
   Array(); // default constructor
   int getSize() const; // return size

   static int getArrayCount();
   void inputArray();

   const T &operator=( const Array & ); // assignment operator
   bool operator==( const Array & ) const; // equality operator

   // inequality operator; returns opposite of == operator
   bool operator!=( const Array &right ) const  
   { 
      return ! ( *this == right ); // invokes Array::operator==
   } // end function operator!=
   
   // subscript operator for non-const objects returns modifiable lvalue
   T &operator[]( int );              

   // subscript operator for const objects returns rvalue
   T operator[]( int ) const;  
private:
   int size; // pointer-based array size
   int *ptr; // pointer to first element of pointer-based array
   static int arrayCount;
}; // end class Array 


Here's my constructor for the template class.

1
2
3
4
5
6
7
8
9
10
11
12
// default constructor for class Array (default size 10)
template <typename T, int arraySize> 
Array<T,arraySize>::Array()
	: size( arraySize )
{
	ptr[arraySize]; // create space for pointer-based array
	
	cout << ptr[0] << endl;

	arrayCount++;

} // end Array default constructor 


Here's the code I was given for the constructor when it was a non-template class.

1
2
3
4
5
6
7
8
9
// default constructor for class Array (default size 10)
Array::Array( int arraySize )
{
   size = ( arraySize > 0 ? arraySize : 10 ); // validate arraySize
   ptr = new int[ size ]; // create space for pointer-based array

   for ( int i = 0; i < size; i++ )
      ptr[ i ] = 0; // set pointer-based array element
} // end Array default constructor 


How do I make the constructor for my template class perform the same job as the previous constructor without the new operator and to make sure it is initialised appropriately for what the T type is?

Sorry if I'm not explaining this very well.

Cheers,

Sean
You want a pointer based array.
Pointer based array points to something.
That something can be allocated either dynamically or statically.
If you allocate a static array in the constructor and set a pointer to it, when constructor ends, the memory will be deallocated, so you can't do that.
You could allocate the static array in the scope of the function which uses your class, but that would defeat the purpose of having that class at all.
You don't want to use new, so dynamic allocation is impossible.

Thus, clearly, a pointer based array is not what you need.
The only other option is what I wrote in my previous post.
Ok. I changed it as you suggested and yay it works. I ditched the int pointer. It works great for inputting and outputting ints

Array< int, 5 > intArray;

but it doesn't for strings. When I output I don't get anything.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
template <typename T, int size>
void Array<T,size>::inputArray()
{
	int arrayIndex, x;

	for( arrayIndex = 0; arrayIndex < size && cin >> x; arrayIndex++ )
    {
		cout << "X is: " << x << endl;
		arr[arrayIndex] = x;
	}
}

template <typename T, int size>
void Array<T,size>::outputArray()
{
	cout << "Size is: " << size << endl;

	for( int arrayIndex = 0; arrayIndex < size; arrayIndex++ )
    {
		cout << "Array Index is: " << arrayIndex << endl;

		cout << arr[arrayIndex];

		if( arrayIndex + 1 != size )
		{
			cout << " ";
		}
	}

	cout << endl;
}


This line:

 
cout << arr[arrayIndex];


output an integer but not a string.

Any ideas? My array is declared as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
template <class T, int size> class Array
{
   friend ostream &operator<<( ostream &, const Array & );
   friend istream &operator>>( istream &, Array & );
public:
   Array(); // default constructor
   int getSize() const; // return size

   static int getArrayCount();
   void inputArray();
   void outputArray();

   const T &operator=( const Array & ); // assignment operator
   bool operator==( const Array & ) const; // equality operator

   // inequality operator; returns opposite of == operator
   bool operator!=( const Array &right ) const  
   { 
      return ! ( *this == right ); // invokes Array::operator==
   } // end function operator!=
   
   // subscript operator for non-const objects returns modifiable lvalue
   T &operator[]( int );              

   // subscript operator for const objects returns rvalue
   T operator[]( int ) const;  
private:
   static int arrayCount;
   T arr[size];
}; // end class Array 


Thanks,

Sean
int arrayIndex, x; ¿What type is x? ¿what type it should be?
Topic archived. No new replies allowed.