Like Tree1Likes
  • 1 Post By Salem

Function template

This is a discussion on Function template within the C++ Programming forums, part of the General Programming Boards category; IS IT NECCESSARY TO PASS ARGUMENTS TO FUNCTION TEMPLATES. CAN't we set it to void.???????????...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    5

    Question Function template

    IS IT NECCESSARY TO PASS ARGUMENTS TO FUNCTION TEMPLATES.
    CAN't we set it to void.???????????

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Dweeb dojha00's Avatar
    Join Date
    Feb 2012
    Location
    Global
    Posts
    23
    As Salem has given u link first read that...
    Go and search it on net before asking here..
    What is life??

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    template<class T>void add(void)
    this is what i want to do is it possible.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    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.
    iMalc likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    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()

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    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
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    thanks salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-30-2011, 03:28 PM
  2. template parameter on a function that dont use the template
    By underthesun in forum C++ Programming
    Replies: 1
    Last Post: 06-22-2009, 05:38 PM
  3. template function v.s. template class
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2007, 12:46 AM
  4. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  5. Function template in Class template?
    By Aidman in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2003, 08:50 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21