I'm working on a large project to emulate some functions of a bank. However, I've run into a strange problem that I can't track down. I was wondering if anyone would be willing to take a look at the program. The problem arose when I tried to add save and load functionality to the bank class. The save and load methods work fine, but the copying of the bank class itself causes the program to crash. I managed to make a small program that displays the problem I'm having.
#include "mathhead200/String.h"
#include "mathhead200/array/Array.h"
#include "bank/Bank.h"
#include "bank/Account.h"
#include "bank/CheckingAccount.h"
usingnamespace mathhead200;
usingnamespace array;
usingnamespace bank;
int main(int arc, char *argv[]) {
//open bank 8642
Bank bank(8642);
//add accounts A-J to the bank
for( int i = 1; i <= 10; i++ )
bank.add( new CheckingAccount("", ((String)"Account ") + (char)(i + 64)) );
//view the accounts in the bank
const Array<Account*> accArr = bank.getAccounts();
for( int i = 0; i < accArr.getSize(); i++ )
say( accArr.get(i)->getName() + ": #" + (int)accArr.get(i)->getNumber() );
//copy the bank, CRASH!
Bank bankCopy = bank;
return 0;
}
If anyone is willing to look at my source code, I can send it to you via email, in a zip file. When I get home I'll see if I can post it to my server so you can download it also.
I'm stuck on this and can't really continue until I figure out why coping a bank instance is messing up my program. If anyone has any ideas, please help!
The bank stores its info in two hashes. I think copying them is what's causing the problem, but when I make another program creating hashes of the same types, I can copy them fine...!
Ha ha, true kbw... But w/e, you understand what's happening. The account names aren't really that important anyway...I posted the source code online, http://mathhead200.com/cpp/major_project.zip
There are a lot of dependencies on other files (classes) I wrote, so I can really post them all here. Please take a look if you think you can help.
PS: The function Bank::getAccounts() doesn't return a reference. It makes a call to Hash<Integer, Account*>::values() which again, also doesn't return a reference.
The function Bank::getAccounts() doesn't return a reference.
Maybe it should.
I've had a quick look at your bank copy constructor.
1. Use initialiser lists to initialise your member containers.
2. Take another look at your Pair. You haven't defined the assignment operator correctly. The compiler will generate it's own. Acutally, you should let the compiler use it's own as your attempt matches the trivial case.
3. Point 2 applies to Node.
4. What do you have against the standard library?
I'm reinventing the wheel so I understand how it works!
Plus, I like Java-style strings better (o_0) Shh... Maybe I shouldn't have said that here... *whistle... Look a pointer! *Runs away
I'll take a look, I added those copy constructors and assignment operators later, I don't think I actually ever invoke them... Again I'll take a look.
Oo! Found a typo on line 18 above, change to ... Array<Pair>::operator=(n); ...
I changed line 16 above to Node(const Node &n) : Array<Pair>(n) { code = n.code; }
What is Array<Pair>::operator = supposed to do anyway? If it's static function, it needs to arguments and if it isn't you need an object to use it on...
It's being invoked on *this, this->Array<Pair>::operator(n);
I was under the impression that this is how you emulated behavior like super.someMethod(); in Java. Did I do this wrong? (I want to invoke the operator= method in my parent class, but without causing recursion.)
Ah...actually, the default operator = will do all that for you, as will the default copy-ctor, so you don't even need to define those functions. If you want to do it yourself, you might have to put the this-> in front of it. What is the error/unexpected result you are getting exactly, anyway?
The program stops working (hangs, i.e. "Windows is checking for a solution to the problem") when the a Bank instance is copied.
Line 26 in my example program "Major Project/src/test6.cpp" (see my first post.)