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'

Templates allow you to condense the example you gave into a more generic approach.
The fact that you are both filling and reversing your vectors within you reverse* calls prevents you from doing this directly.
Idealy, what you want is to combine the duplicated code between the two versions you have in to a templated version. Something like the following:

template< typename Type >
void reverseAny (std::vector< Type >& vec) {
   std::reverse (vec.begin (), vec.end ());
}

Then you would have alternate fill and print versions. Maybe something like:

template< typename Type >
printAny (const std::vector< Type >& vec) {
   std::vector< Type >::iterator vit = vec.begin ();
   for (; vit != vec.end (); ++vit) 
      std::cout << *vit << ' ';
   std::cout << std::endl;
}

Now you can do things like

std::vector< int > vi;
std::vector< std::string > vs;

// fill both vectors

reverseAny (vi); 
reverseAny (vs);

printAny (vi);
printAny (vs);

And the proper versions of those functions are created (at compile time) and executed (at run time).

But here, I need to push_back some values which whihc wouldn't be possible as in the code mentioned above...

But here, I need to push_back some values which whihc wouldn't be possible as in the code mentioned above...

Hence my comment in the code: // fill both vectors I'm not going to write the entire thing for you.

You can either draw up a templatized version of fillAny using the above as a guide or you can do it by hand as you have done in your previous attempt.

What I provided is a 80% working example. Use that to play with and understand what is going on and finish the other 20% once you do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.