Class Templates and member functions

This is a discussion on Class Templates and member functions within the C++ Programming forums, part of the General Programming Boards category; Hello, I am trying to convert a regular class to a templated one. I thought this would be simple enough, ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    Class Templates and member functions

    Hello,

    I am trying to convert a regular class to a templated one.

    I thought this would be simple enough, but now I my functions do not compile.

    Where have I cone wrong?


    Code:
    template<class T>
    class BankAccount
    { private:
        string CustFirst;
        string CustLast;
        int    CustAccNum;
        int    PinNum;
        T      AccBalance;
    
        public:
    
          void openAccount(int index);
    };
    ~~ Program Body ~~

    Code:
    void BankAccount::openAccount(int index)
    { int AccNum = 100100 + index;
    
      system("cls");
      cout<< "********** Bank of Regis **********" << endl << endl;
      cout<< " New Account Number < " << AccNum << " > " << endl << endl;
      cout<< " Enter customers first name: " << endl;
      cout<< " ->: " ;
      cin>> CustFirst;
      cout<< " Enter customers last name: " << endl;
      cout<< " ->: " ;
      cin>> CustLast;
      cout<< " Enter customers PIN number: " << endl;
      cout<< " ->: " ;
      cin>> PinNum;
      cout<< " Enter inital deposite amount: " << endl;
      cout<< " ->: $";
      cin>> AccBalance;
    
      CustAccNum = AccNum;
    
      system("cls");
      cout<< "********** Bank of Regis **********" << endl << endl;
      cout<< "          Account Summary          " << endl;
      cout<< " Account# " << CustAccNum << endl;
      cout<< " Customer " << CustFirst << " " << CustLast << endl;
      cout<< " Balance  $" << AccBalance << endl << endl;
      cout<< "***********************************" << endl;
      system("pause");
     }

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    1 . why are you making this class a template? I see no design need for genericity here. Or are you just doing it for the practice?

    2. what errors are you getting?

    most likely you've put the method body in a cpp file and unless you're one of the 0 people on this board using the comeau compiler that won't work.

    BTW as a general design principle, you should seperate model classes (i.e class that represent the appliction logic, i.e. a BankAccount class) from user interface code.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Code:
    template<class T>
    void BankAccount<T>::openAccount(int index)
    You have to template the function as well.
    Last edited by SlyMaelstrom; 02-22-2006 at 06:30 PM.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    I concede that templating the class is over kill, and a bit pointless, but I am trying to learn. Baby steps and all.

    I tried telmplating the functions as suggested, but still got errors. Here is the code in it entirety. I commented out the templating because of the errors.

    I would appreciate it if you could take a look at it.

    I am using Dev C++

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    const int MaxSize = 100;     // Size of account database
    
    //template<class T>
    class BankAccount
    { private:
        string CustFirst;
        string CustLast;
        int    CustAccNum;
        int    PinNum;
    //    T      AccBalance
        double AccBalance;
    
        public:
          void openAccount(int index);
          void accDeposite();
          void accWithdraw();
          void dsplyAcc();
          bool operator==(int find);
    
    };
    
    int main()
    {  char selection = '\0';
       int  exit = 0;
       int  index = 0;
       int  find = '\0';;
    //   BankAccount<double> AccountDatabae[MaxSize];
       BankAccount *AccountDatabase[MaxSize];
    
       do
        { system("cls");
          cout<< "********** Bank of Regis **********" << endl << endl;
          cout<< " 1. Open Account                   " << endl;
          cout<< " 2. Make a witdraw                 " << endl;
          cout<< " 3. Make a deposite                " << endl;
          cout<< " 4. View Account                   " << endl;
          cout<< " 5. Exit                           " << endl << endl;
          cout<< "***********************************" << endl;
          cout<< " ->: ";
          cin>> selection;
    
          switch(selection)
           { case '1':
                 if(index<MaxSize)
                  { //AccountDatabase[index] = new BankAccount<doubld>;
                    AccountDatabase[index] = new BankAccount;
                    AccountDatabase[index] -> openAccount(index);
                    ++index;
                  }
               break;
    
             case '2'://~~~~~~~~~~~~~~ Account Withdraw
               system("cls");
               cout<< "********** Bank of Regis **********" << endl << endl;
               cout<< " Enter your account number " << endl;
               cout<< " ->: ";
               cin>> find;
               for(int i=0; i<index; ++i)
                 { if(*AccountDatabase[i]==find)
                     { AccountDatabase[i]-> accWithdraw();
                       break;
                     }
                 }
               find = '\0';
               break;
    
             case '3'://~~~~~~~~~~~~~~ Account Deposite
               system("cls");
               cout<< "********** Bank of Regis **********" << endl << endl;
               cout<< " Enter your account number " << endl;
               cout<< " ->: ";
               cin>> find;
               for(int i=0; i<index; ++i)
                 { if(*AccountDatabase[i]==find)
                     { AccountDatabase[i]-> accDeposite();
                       break;
                     }
                 }
               find = '\0';
               break;
    
             case '4'://~~~~~~~~~~~~~~ Account View
               system("cls");
               cout<< "********** Bank of Regis **********" << endl << endl;
               cout<< " Enter your account number " << endl;
               cout<< " ->: ";
               cin>> find;
               for(int i=0; i<index; ++i)
                 { if(*AccountDatabase[i]==find)
                     { AccountDatabase[i]-> dsplyAcc();
                       break;
                     }
                 }
               find = '\0';
               break;
    
             case '5'://~~~~~~~~~~~~~~ Program Exit
                exit = 1;
               break;
    
             default:
                { cout<< " You have made and invalid selection " << endl << endl;
                  system("pause");
                break;
                }
           }
        }while(exit != 1);
    
      return 0;
    }
    
    //template<class T>
    void BankAccount::openAccount(int index)
    { int AccNum = 100100 + index;
    
      system("cls");
      cout<< "********** Bank of Regis **********" << endl << endl;
      cout<< " New Account Number < " << AccNum << " > " << endl << endl;
      cout<< " Enter customers first name: " << endl;
      cout<< " ->: " ;
      cin>> CustFirst;
      cout<< " Enter customers last name: " << endl;
      cout<< " ->: " ;
      cin>> CustLast;
      cout<< " Enter customers PIN number: " << endl;
      cout<< " ->: " ;
      cin>> PinNum;
      cout<< " Enter inital deposite amount: " << endl;
      cout<< " ->: $";
      cin>> AccBalance;
    
      CustAccNum = AccNum;
    
      system("cls");
      cout<< "********** Bank of Regis **********" << endl << endl;
      cout<< "          Account Summary          " << endl;
      cout<< " Account# " << CustAccNum << endl;
      cout<< " Customer " << CustFirst << " " << CustLast << endl;
      cout<< " Balance  $" << AccBalance << endl << endl;
      cout<< "***********************************" << endl;
      system("pause");
     }
    
    //template<class T>
    void BankAccount::accDeposite()
    { double deposite = 0.00;
      int temp = '\0';
    
      cout<< " Enter your PIN " << endl;
      cout<< " ->: ";
      cin>> temp;
    
      if(temp == PinNum)
        { do
           { system("cls");
             cout<< "********** Bank of Regis **********" << endl << endl;
             cout<< " Deposite Amount                   " << endl;
             cout<< " ->: $";
             cin>> deposite;
             if(deposite<0)
               { cout<< " Invalid deposite amount" << endl << endl;
                 system("pause");
               }
              else
                { AccBalance+=deposite;
                }
            }while(deposite<0);
        }
        else
          { cout<< " Invalid PIN" << endl << endl;
            system("Pause");
          }
      temp = '\0';
    }
    
    //template<class T>
    void BankAccount::accWithdraw()
    { double amt = 0.00;
      int temp = '\0';
    
      cout<< " Enter your PIN " << endl;
      cout<< " ->: ";
      cin>> temp;
    
      if(temp == PinNum)
        { do
           { system("cls");
             cout<< "********** Bank of Regis **********" << endl << endl;
             cout<< " Withdraw Amount " << endl;
             cout<< " ->: $";
             cin>> amt;
             if(amt>AccBalance)
               { cout<< " You can not withdraw more than" << endl;
                 cout<< " $" << AccBalance << endl<< endl;
                 system("pause");
               }
              else
                { AccBalance -= amt;
                  amt = '\0';
                }
           }while(amt>AccBalance);
        }
        else
          { cout<< " Invalid PIN " << endl << endl;
            system("pause");
          }
      temp = '\0';
    }
    
    //template<class T>
    void BankAccount::dsplyAcc()
    { int temp = '\0';
    
      cout<< " Enter your PIN " << endl;
      cout<< " ->: ";
      cin>> temp;
    
      if(temp == PinNum)
        {  system("cls");
           cout<< "********** Bank of Regis **********" << endl << endl;
           cout<< "          Account Summary          " << endl;
           cout<< " Account# " << CustAccNum << endl;
           cout<< " Customer " << CustFirst << " " << CustLast << endl;
           cout<< " Balance  $" << AccBalance << endl << endl;
           cout<< "***********************************" << endl;
           system("pause");
        }
        else
          { cout << " Invalid PIN " << endl << endl;
            system("pause");
          }
        temp = '\0';
    }
    
    //template<class T>
    bool BankAccount::operator==(int find)
    { if(CustAccNum == find)
        { return(1);
        }
       else
         { return(0);
         }
    }

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    When you template a class and it's objects, you have to address the template everytime you address it.

    Code:
    template<class Foo>  // Now the class is being templated
    class DataContainer {
         public:
             DataContainer();
             void somefunc();
    };
    
    /* Now lets template the functions. Don't forget the class is templated, therefor the scope must also use the templalte */
    
    DataContainer<Foo>::DataContainer() {}     /* Look at the template before the scope resolution operator */
    
    void DataContainer<Foo>::somefunc() {}
    Look at this code: http://www.cprogramming.com/tutorial...stackcode.html

    This is properly using templates.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    First off. Thank your for your help and patience.

    I have updated my off lined functions with the syntax.

    Code:
    void BankAccount<T>::Function()
    {~~Foo~~; }
    I am lost when it comes to scope.

    Do I need to add

    Code:
    BankAccount<T>::BankAccount() {}
    in the the BankAccount Class or where?

    I am sorry that I am slow on the uptake when it comes to this stuff.

    Regards,

    Rich

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Yes. The reason being is when you call the scope, you're calling the class. Just like when you create an object.

    Code:
    BankAccount<int> obj1;
    You have to give the template a value. In the case of the member functions (including the constructor, destructor, and copy constructor) your templates value is the template name. See how they do it in the link?
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I am lost when it comes to scope.
    Think of it this way: every member function you define outside a class has to identify which class it belongs to. You attempted to do that here:
    void BankAccount::openAccount(int index)
    However, there is no class called BankAccount. For there to be a class called BankAccount in your program, there would have to be a declaration like this somewhere in your code:
    Code:
    class BankAccount
    {
    };
    However, there isn't. There is this statement:
    Code:
    template<class T>
    class BankAccount
    {
    };
    But those are not equivalent. Since you decided to template your class, there will only be classes like "BankAccount for type int" and "BankAccount for type float". There will never be a class called just "BankAccount", and you will never be able to declare an object like this:
    Code:
    BankAccount myAcc;
    C++ has very strict syntax, and you must follow it exactly. To try and learn a new topic and just guess at the syntax usually won't be very successful. Furthermore, when you are learning a new topic, you shouldn't bury it 100's of lines of irrelevant code. The reasonable thing to do is to create a very simple test class to learn the syntax. The class should have only a couple of member variables, a constructor, and one function to display the member variables. That's it.
    Last edited by 7stud; 02-23-2006 at 12:50 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 07-24-2006, 09:14 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  3. Templates for member functions
    By reachb4 in forum C++ Programming
    Replies: 1
    Last Post: 07-01-2005, 05:53 AM
  4. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 03:46 AM

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