Overloading template functions

I need a little bit of help with overloading template functions. I have the following program:

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
32
33
34
35
#include <iostream>

using namespace std;

template <typename T>

void printArray( const T * const array, int count)
{
	for ( int i = 0; i< count; ++i)
	 cout << array[i] << " ";

	cout << endl;
}

int printArray( const T * const array, int lowSubscript, int highSubscript)
{
	int lowSubscript;
	int highSubscript;

	if (highSubscript > lowSubscript)

		for ( int i = lowSubscript; i < highSubscript; ++i)
		{
			cout << array[i] << " ";

			cout << endl;
		}

	
	if (lowSubscript > highSubscript)

		return 0;
	

}


The first function works fine and I am able to print using it. I am trying to overload it by passing two integers that are supposed to be part of an array. The function should print the elements located in between those two values and return 0 if the low value is higher than the high value.

The following are the errors I am getting:

1
2
3
fig14_01.cpp(15) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
fig14_01.cpp(15) : error C2143: syntax error : missing ',' before '*'
fig14_01.cpp(24) : error C2065: 'array' : undeclared identifier


Any help would be appreciated.
Last edited on
It would probably help if you wrote it like this:
1
2
3
4
5
6
7
8
template <typename T>
void printArray( const T * const array, int count)
{
	for ( int i = 0; i< count; ++i)
	 cout << array[i] << " ";

	cout << endl;
}
or like this:
 
template <typename T> void printArray( const T * const array, int count)
Some people prefer the latter, but personally, I find it harder to read.

The template part applies only to the declaration that comes directly after it, not to every declaration in the file.
Helios,

thanks for the advice. I added the following line template <typename T> to the second function, it compiled and the most important part: it worked.

I didn't know I have to use that line in each new function that I write.

Thanks!!!
Topic archived. No new replies allowed.