Logical Error

Pages: 123
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.

File: "Major Project/src/test6.cpp"
Download source code: http://mathhead200.com/cpp/major_project.zip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#include "mathhead200/String.h"
#include "mathhead200/array/Array.h"
#include "bank/Bank.h"
#include "bank/Account.h"
#include "bank/CheckingAccount.h"

using namespace mathhead200;
using namespace array;
using namespace 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.
Last edited on
Sounds like you aren't copying your pointers correctly, i.e. you are only making shallow copies.
I don't want to make deep copies of the pointer.

How can, or would that make the program crash (stop working, hang, "Windows is checking for a solution to the problem")?
Last edited on
If anyone is willing to help me with this I have uploaded the source code in a zip file:
http://mathhead200.com/cpp/major_project.zip

Let me know if you figure anything out, or if you think you might know what the problem is.
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!
Actually, you would have to like this, otherwise your destructors are probably going to be deleteing the objects you new'd twice.
...you would have to like this...
"like" what?

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...!
Last edited on
This:
1
2
3
	//add accounts A-J to the bank
	for( int i = 1; i <= 10; i++ )
		bank.add( new CheckingAccount("", ((String)"Account ") + (char)(i + 64)) );

could be written clearer as:
1
2
3
	//add accounts A-J to the bank
	for (char i = 'A'; i <= 'J'; ++i)
		bank.add(new CheckingAccount("", ((String)"Account ") + i );


My guess is that you've missed the reference in:
 
	const Array<Account*> &accArr = bank.getAccounts();


Finally, it's impossible to tell why your copy constructor crashes the app without seeing it.
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.
Last edited on
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?
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.
Last edited on
What's wrong with how I implemented Hash<K, T>::Pair::operator= and Hash<K, T>::Node::operator=

File: "Major Project/src/mathhead200/hash/Hash.h"
Download source code: http://mathhead200.com/cpp/major_project.zip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template <typename K, typename T> class Hash
{
	struct Pair {
		K key;
		T value;
		//...
		Pair(const Pair &p) { key = p.key; value = p.value; }
		//...		
		Pair& operator=(Pair p) { key = p.key; value = p.value; return *this; }
		//...
	};
	
	struct Node : Array<Pair> {
		int code;
		//...
		Node(const Node &n) { Array<Pair>::operator=(n); code = n.code; }
		//...
		Node& operator=(Node n) { Array<Pair>::operator(n); code = n.code; return *this; }
		//...
	};
	//...
};
//... 


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; }
Last edited on
I'm still stuck on this error... Bump!
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.)
Last edited on
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.)
Last edited on
Still seeking help!
Thanks to firedraco, and kbw for your help so far. It's much appreciated!
Bump.
Still need help, anyone?
Pages: 123