# Function template

Printable View

• 11-11-2012
kshanuanand
Function template
IS IT NECCESSARY TO PASS ARGUMENTS TO FUNCTION TEMPLATES.
CAN't we set it to void.???????????
• 11-11-2012
Salem
• 11-11-2012
dojha00
As Salem has given u link first read that...
Go and search it on net before asking here..
• 11-11-2012
kshanuanand
template<class T>void add(void)
this is what i want to do is it possible.
• 11-11-2012
Salem
Surely that depends on what your template add() actually does.

Rather than asking "is it possible", why don't you try it and post your example (and error messages).

Because there is no point in us coming up with an example that works, if it looks nothing like what you're trying to do.
• 11-12-2012
kshanuanand
here is the program-:
Code:

```/*WAP (c++) to convert rupees to dollar and vice-versa.*/ #include<iostream> using namespace std; template<class T>void rtd() {   T d,r;   cout<<"Enter the AMOUNT in Rupees-: ";   cin>>r;   d=r/54.5;   cout<<"Amout in Dollar-: "<<d<<endl; } template<class T>void dtr() {   T r,d;   cout<<"Enter the AMOUNT in Dollar-: ";   cin>>d;   r=d*54.5;   cout<<"Amout in Ruppes-: "<<d<<endl; } int menu(void) {   int o;   cout<<"1.Convert Rupees To Dollar.\n2. Convert Dollar To Rupees.\n3 Exit.\nSelect an Option-: ";   cin>>o;   switch(o)   {     case 1:rtd();break;     case 2:dtr();break;     case 3:return 0;   }   menu(); } int main() {   menu();   return 0; }```
and the error message-:

Q10.cpp: In function ‘int menu()’:
Q10.cpp:27:16: error: no matching function for call to ‘rtd()’
Q10.cpp:27:16: note: candidate is:
Q10.cpp:4:23: note: template<class T> void rtd()
Q10.cpp:28:16: error: no matching function for call to ‘dtr()’
Q10.cpp:28:16: note: candidate is:
Q10.cpp:12:23: note: template<class T> void dtr()
• 11-12-2012
Salem
You have to invoke an instantiation of your template.
Templated functions don't have a separate existence.

So for example
Code:

```    case 1:rtd<int>();break;     case 2:dtr<int>();break;```
Where I get
Code:

```\$ g++ bar.cpp \$ ./a.out 1.Convert Rupees To Dollar. 2. Convert Dollar To Rupees. 3 Exit. Select an Option-: 1 Enter the AMOUNT in Rupees-: 1234 Amout in Dollar-: 22 1.Convert Rupees To Dollar. 2. Convert Dollar To Rupees. 3 Exit. Select an Option-: 3```
• 11-12-2012
kshanuanand
thanks salem