I ma brand new to templates and I have been given an exercise to reverse the elements using vectors which I did perfectly. But I need to include the template thing which I tried my level best to understand and do but still I am getting many errors. Any help is greatly appreciated here. By the way, I need to have anm argument inside the function which I am unable to put it.
#include<iostream>
#include<string>
#include<conio.h>
//#include"revElement.h"
using namespace std;
template<class elemType>
void reverseInt();
void reverseString();
int main()
{
//revElement r;
reverseInt();
cout<<endl;
cout<<endl;
reverseString();
getch();
return 0;
}
template<class elemType>
void reverseInt()
{
vector<int>intList;
vector<int>::iterator i;
intList.push_back(1);
intList.push_back(2);
intList.push_back(3);
intList.push_back(4);
intList.push_back(5);
reverse(intList.begin(),intList.end());
cout<<"The reversed integers are: "<<endl;
for(i=intList.begin();i!=intList.end();++i)
{
cout<<" "<<*i;
}
}
template<class elemType>
void reverseString()
{
vector<string>stringList;
vector<string>::iterator s;
stringList.push_back("This");
stringList.push_back("is");
stringList.push_back("my");
stringList.push_back("C++");
stringList.push_back("Lab");
reverse(stringList.begin(),stringList.end());
cout<<"The reversed strings are: "<<endl;
for(s=stringList.begin();s!=stringList.end();++s)
{
cout<<" "<<*s;
}
}
Here are the errors that I am getting:
error C2783: 'void reverseInt(void)' : could not deduce template argument for 'elemType'
see declaration of 'reverseInt'