IS IT NECCESSARY TO PASS ARGUMENTS TO FUNCTION TEMPLATES.
CAN't we set it to void.???????????
IS IT NECCESSARY TO PASS ARGUMENTS TO FUNCTION TEMPLATES.
CAN't we set it to void.???????????
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
As Salem has given u link first read that...
Go and search it on net before asking here..
What is life??
template<class T>void add(void)
this is what i want to do is it possible.
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
here is the program-:
and the error message-: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; }
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()
You have to invoke an instantiation of your template.
Templated functions don't have a separate existence.
So for example
Where I getCode:case 1:rtd<int>();break; case 2:dtr<int>();break;
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
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
thanks salem