Problem with function template

Hi,

I am getting these errors:
error: variable or field 'reversal' declared void
error: 'aType' was not declared in this scope

my code is to take any type of a pointer to an array of elements and reverse the contents of the array.

At the top of my code I have:
template <class aType>
void reversal(aType);

my driver code just calls the reversal function with a parameter. One is a char, the next time it is called it is called with a bool parameter, and then a short.

my function definition, which is the line my compiler is giving me errors on looks like this...

void reversal(aType myType)
{
aType holder;
int backend = mySize;
for (int i = 0; i <= 12; i++)
{
--backend;
holder = myShort[i];
myShort[i] = myShort[backend];
myShort[backend] = holder;
}
}

What are these errors telling me. My teacher did not provide any lectures on templates yet is asking us to use them so I just looked up a tutorial but Im clearly not doing it right.

Thanks.
The definition needs the template<typename aType> part as well.

Also, if you separate the definition of the function template into its own .cpp file, it won't compile because the full definition needs to be known for a template to be instantiated.

For simplicity, just define the full function at the top of your code (or at some point before using it).

1
2
3
4
5
template <typename aType>
void reversal(aType myType)
{
    // ...
}

PS: an object is not a type. the name "myType" is not a good name for a variable. Also, if you can't think of a better name for the typename, you might as well just leave it as the classic "T", instead of "aType". But this PS part of my post is more opinion than rules.

Your function template doesn't even use the "myType" parameter, so I don't know why it's there.
Last edited on
Thanks! that worked.
Topic archived. No new replies allowed.