accessing the data of a template class

Feb 16, 2012 at 5:26pm
we've to create an array template class that can be an array of any type. starting off i've created an int template, and using addEntry(), it compiles okay. i'm wondering how do i code my function to access/get total value/print out elements of the array? i've tried using -> but it won't work.

i have T *theArray, as the array, and it's initialised like this:

1
2
3
4
5
6
7
8
9
template <typename T>
class arrayT
{
public:
	arrayT(int s)
	{
		size = s;
		theArray = new T [size];
	}


our second function needs to find the total value of the elements in the array; so how do i code that function, without knowing what data type the template will be used for in advance? in the print/get total value etc function can i go T values;, and then vlaues will become whatever data type the template is used as?

Feb 16, 2012 at 6:13pm
You need to show more of what you've done so we can see where the problem is.
Feb 16, 2012 at 6:27pm
If your talking about something like...??

Somewhere in main...
1
2
3
4
5
6
7
8
9
//...
arrayT<int> intArray(100);
//... fill up the array.
for(int i = 0; i < 100; ++i)
{
   //Do something with values...
   //Print out values ... intArray[i]
}


You can add whatever you want to your template class as if it were a normal class and use it as such. Obviously your implementation needs to work for any given type (and you might need to specialize) but if all you are doing is primitive types you can probably get a way with just the template.

You will need to add that functionality to your class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <typename T>
class arrayT
{
public:
	arrayT(int s)
	{
		size = s;
		theArray = new T [size];
	}
       T getTotalValue()const;
       cont T& operator[](std::size_t pos)const
       {
          //....
          return theArray[pos];
       }
Last edited on Feb 16, 2012 at 6:32pm
Feb 16, 2012 at 8:55pm
our second function needs to find the total value of the elements in the array; so how do i code that function, without knowing what data type the template will be used for in advance? in the print/get total value etc function can i go T values;, and then vlaues will become whatever data type the template is used as?


You require operator+ (or possibly +=) to be valid for the types used for your arrayT. Both are valid for every built-in numeric type, and can be overloaded for user defined types.

Then, in your method utilize the operator to add 'em up.
Feb 21, 2012 at 9:04pm
okay i think i'm almost there...having one problem though, in my totalValue() function, i have a variable T total;. when i run the program i'm getting an error saying the variable hasn't been initialised. i've tried going T total=0; with no luck. is there something i'm missing regarding template variables? here's the function:

1
2
3
4
5
6
7
8
template <typename T, int capacity>
T& arrayT<T, capacity>::totalValue() const
{
	T total;
	for(int i = 0; i < next; i++)
		total = total + theArray[i];
	return total;
}
Feb 21, 2012 at 9:54pm
Are you getting an error or a warning? initializing total to 1 should be ok, at least with my compiler it is. You are returning a reference to a local variable, you will need to return that by value.
Feb 22, 2012 at 9:32am
i changed return type from T& to void, as so:

1
2
3
4
5
6
7
8
template <typename T, int capacity>
void arrayT<T, capacity>::totalValue()
{
	T total = 0;
	for(int i = 1; i < next; i++)
		total = total + theArray[i];
	cout<<"Total value of array elements: "<<total<<endl;
}


and here's what i'm doing in main.cpp:
1
2
3
4
5
6
7
8
9
arrayT<int, 10> theArray;

	theArray.addEntry(5);
	theArray.addEntry(5);
	theArray.addEntry(5);
	theArray.addEntry(5);
	theArray.addEntry(5);

	theArray.totalValue();


it's running okay but the output i'm getting from the console window is just '26'...even though i have "Total value....." printing from the function? should it not be 25, and why isn't it including the rest of what's in the cout in the function?
Feb 22, 2012 at 9:18pm
I don't know why it is 26 from looking at what you've posted, what is next, what is the array initialized to, I see you have size of 10 but you only add 5 whose value is 5. Also try cleaning and rebuilding the solution.
Topic archived. No new replies allowed.