Thread: hey all for the second time this week

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    hey all for the second time this week

    here's the deal. i tried implementing classes in the way that i was questioning the other day, and here's the code i got:
    Code:
    #ifndef LOGIN_H
    #define LOGIN_H
    
    #include <iostream>
    #include <fstream.h>
    #include <string>
    #include <stdlib.h>
    #include <ctype.h>
    
    using namespace std;
    
    class BankClass
    {
     private:
     double balance;
    
     public:
     class BalanceOps
     {
      public:
      BalanceOps ()
      {
       balance = 0.0;
      }
      void credit (double amt)
      {
       balance += amt;
      }
      void debit (double amt)
      {
       balance -= amt;
      }
      double getBalance ()
      {
       return balance;
      }
      void setBalance (double amt)
      {
       balance = amt;
      }
     };
     class FileOpsBank
     {
      private:
      string username;
      string usersdir;
      double balance;
    
      public:
      FileOpsBank (string user, string dir)
      {
       username = user;
       usersdir = dir;
      }
      void loadBalance (string filename)
      {
       ifstream infile;
       string tempstr;
    
       infile.open(filename.c_str());
       getline (infile, tempstr);
       infile.close ();
       balance = strtod (tempstr.c_str(), NULL);
      }
      void saveBalance (string filename)
      {
       ofstream outfile;
       outfile.open (filename.c_str());
       outfile << balance;
       outfile.close ();
      }
     };
    };
    
    int main ()
    {
     BankClass::BalanceOps banker;
     double amt;
    
     banker.setBalance (2.25);
     cin >> amt;
     banker.credit (amt);
     amt = banker.getBalance ();
     cout << amt;
     system ("pause");
     return 0;
    }
    
    #endif
    the main function here was just to test the class, not my actual program. Anyways, when i compile the code as is with Dev C++, it says "linker error: undefined reference to BankClass::balance". What is going on here? oh, and i know i have a bunch of unneccesary libraries included, i promise, im using them later.
    Last edited by xixpsychoxix; 05-31-2007 at 07:00 AM. Reason: left stuff out

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Your code has many issues.

    1. Just because a class is embedded in another class, does not mean it has access to that class' private members. You could solve this by making it a friend class.

    2. In the embedded classes you can't just reference balance unqualified. It will look for it's own member called balance, which ofcourse doesn't exist. However you can't say BankClass::balance either as this is treated as a static variable. You need a way to say this->balance where "this" means BankClass and not BalanceOps, but nothing like this exist. The best way to solve this is pass in a instance of BankClass, to each function that needs it, though this is not the best overall solution.

    3. Finally, when you instance your BalanceOps class with BankClass::BalanceOps banker; A BankClass is not automatically created. BankClass and BalanceOps are 2 different entities and are not linked in anyway.

    Basically it boils down to the 1st point I made, embeded class don't gain any special access or abilities by being embeded. Think of a class defined in a class as just a typedef and nothing more.
    Last edited by Darryl; 05-31-2007 at 08:54 AM. Reason: Elaborated

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    By the way, "hey all" is not a good topic title. Try to shortly outline your issue instead.

    As for the issues in your code: you didn't pick up a single word I wrote in your last thread, did you?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    umm

    no, actually i didnt know anyone posted anything more. I never read it, but now i did and i think i get it. I have an idea as to what i did wrong now, at least. thanks again

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    150
    Ok, i got it to work by making BankClass a namespace instead of a class. That works just as well i guess, unless anyone happens to have an extremely good reason why i shouldnt do that. there isnt one, is there?

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yes, there is. What if you want more than one bank?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Also, your code looks like it's meant to be in a header file. You shouldn't put function implementations like main() in header files if you can help it.

    I'm sure that someone has mentioned this, but
    Code:
    #include <fstream.h>
    should be <fstream>. Changing <stdlib.h> to <cstdlib> and <ctype.h> to <cctype> would probably be a good idea too, but not essential.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 03:41 AM
  2. Help with FileIO
    By iiwhitexb0iii in forum C Programming
    Replies: 12
    Last Post: 04-03-2006, 11:11 AM
  3. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 07:56 AM
  4. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM
  5. Checking parts of a structure
    By DocDroopy in forum C Programming
    Replies: 11
    Last Post: 08-05-2002, 07:45 AM