Welcome! Log In Create A New Profile

Advanced

11th Hour Assistance needed

Posted by theopaidion 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
11th Hour Assistance needed
July 30, 2006 02:21PM
Help!

In the withdraw and deposit methods, after processing the deposit/withdrawal itself, a Transaction needs to be added to the Account_impl's Statement, right? But I'm having trouble in adding this. My account_impl.h has a private member


Account:confused smileytatement_var statement;


(I tried merely saying Statement statement, but that gave me an error Account_impl.h:47: 'Statement' is used as a type, but is not defined as a type.)

Now, after processing the withdrawal/deposit, I do the following:


Account::Transaction newTrans;
newTrans.direction = Account::Transaction::withdraw;
newTrans.amount = amount;

CORBA::ULong curLen = statement.length();
statement.length(curLen + 1);
statement[curLen] = newTrans;


Now, according the OMG C++ mapping doc, "The length() functions can be used to access and modify the length of the sequence. Increasing the length of a sequence adds new elements at the tail. The newly-added elements behave as if they are default-constructed when the sequence length is increased.". However, when I try to compile this, I get:

account_impl.cc: In member function `virtual void Account_impl::withdraw(long unsigned int)':
account_impl.cc:71: no matching function for call to ` TSeqVar<SequenceTmpl<Account::Transaction, 0> >::length()'
account_impl.cc:72: no matching function for call to ` TSeqVar<SequenceTmpl<Account::Transaction, 0> >::length(long unsigned int)'


How now? Is there another way I can merely add a member to the sequence? Another way I should get the length of it?

HELP!





Re: 11th Hour Assistance needed
July 30, 2006 02:30PM
Another thing ... in my Bank_impl I have a map to link the names to the accounts. It is declared:


std::map<std::string, Account> nameList;


However, when I try to create a pair to insert into the map, for instance in open()


nameList.insert(pair<string,Account>(name,newAcc));


I get the error:


c:/unisa/mingw/include/c++/3.2.3/bits/stl_pair.h:69: cannot declare field ` std::pair<std::string, Account>::second' to be of type `Account'
c:/unisa/mingw/include/c++/3.2.3/bits/stl_pair.h:69: because the following virtual functions are abstract:
Account.h:131: virtual void Account::deposit(long unsigned int)
Account.h:132: virtual void Account::withdraw(long unsigned int)
Account.h:133: virtual Long Account::balance()
Account.h:134: virtual void Account::destroy()


I can't merely switch this to Account_impl instead, that gives errors too. Putting pointers in the container seems to be discouraged, because of memory management concerns.

Also, I get the error:


account_impl.cc:160: conversion from `Account_impl*' to non-scalar type `Account_impl' requested


from the assignment


Account_impl newAcc = new Account_impl(name, _this());


just before that. This is supposed to create a new account, the constructor for which is


Account_impl(const char* name, BankRef bankRef)


The name is stored in an Account_impl private member along with the pointer to the bank. These are then used later to access the name & password lists to remove the account from it. What am I doing wrong?


Re: 11th Hour Assistance needed
July 30, 2006 03:33PM
Okay, okay. I think I found my errors. For the first post, I stupidly treated statement as a Statement whereas I had declared it as a Statement pointer. I merely dereferenced properly (statement->length() and so on) and that's fine now.

Right, for the second post, I've changed the map to hold Account_impl's instead of merely Account's. Plus, again quite stupidly, I was not keeping tabs on pointers. I was creating a pointer, and then trying to store it as the actual item. This seems solved now.

There is one problem remaining though, and I can't figure out what to do with it. Could anyone help? I get the following gmake error:


Account.h: In copy constructor `Account_impl::Account_impl(const
Account_impl&winking smiley':
c:/unisa/mingw/include/c++/3.2.3/bits/stl_pair.h:84: instantiated from `std::p
air<_T1, _T2>::pair(const _T1&, const _T2&winking smiley [with _T1 = std::string, _T2 = Accou
nt_impl]'
account_impl.cc:165: instantiated from here
Account.h:313: `POA_Account:tongue sticking out smileyOA_Account(const POA_Account&winking smiley' is private
c:/unisa/mingw/include/c++/3.2.3/bits/stl_pair.h:84: within this context
gmake: *** [account_impl.o] Error 1


I don't get it. I did not code a copy constructor ...
Re: 11th Hour Assistance needed
July 30, 2006 03:54PM
... which I have now done. It runs:


Account_impl::Account_impl(const Account_impl &rhs) {
this->name = rhs.getName();
this->bankPtr = rhs.getOwner();

Account:confused smileytatement * oldStat = rhs.getStatement();
Account:confused smileytatement * newStat = new Account:confused smileytatement();

Account::Transaction curTrans;
CORBA::ULong curLen;

for (CORBA::ULong i = 0; i < oldStat->length(); i++) {
curLen = newStat->length();
newStat->length(curLen + 1);
(*newStat)[curLen] = (*oldStat)[curLen];
}
}



The first two assignments are straightforward using accessor methods (the first returns the string name, and the second the BankRef pointer to the owner of the account), the last assignment is a deep copy for the statement.

Okay. But now I get this error:



account_impl.cc: In copy constructor `Account_impl::Account_impl(const Account_impl&winking smiley':
account_impl.cc:27: passing `const Account_impl' as `this' argument of `std::string Account_impl::getName()' discards qualifiers
account_impl.cc:28: passing `const Account_impl' as `this' argument of `Bank_impl* Account_impl::getOwner()' discards qualifiers
account_impl.cc:30: passing `const Account_impl' as `this' argument of `virtualSequenceTmpl<Account::Transaction, 0>* Account_impl::getStatement()' discards qualifiers
gmake: *** [account_impl.o] Error 1




Huh?
Re: 11th Hour Assistance needed
July 30, 2006 04:01PM
You need to put account_impl pointers in the map, not account_impl objects. If you try to put the object in, it tries to create a copy of the object, so then you suddenly have two objects in memory that is the same account. This whould not make sense, since if you for example update the balance, which of the two objects need to be updated etc. Fortunately, the copy constructor is private by default, preventing you from easily making this mistake.
Re: 11th Hour Assistance needed
July 30, 2006 04:02PM
OK, I see your new post. Put as I pointed out, you must not have a copy constructor, or your program will become inconsitent.
Re: 11th Hour Assistance needed
July 30, 2006 04:17PM
(This is fast becoming a monologue of stupidity)

Okay. I've surfed around a bit and discovered that gmake's throwing these errors because I am trying to access non-const members (the accessors) from a const object (rhs). Right, now obviously I can change getName() and getOwner() to const methods easily. Done. No more errors there.

But the error remains for getStatement(), and I can't modify it to a const method because then it does not match up to the original declarations in Account.h and so on and that gives me a host of problems.



Aaargh.


I can't see a way out of this, unless I do not need to code a copy constructor ... but then we're back to the error in my third post above. HELP!
Re: 11th Hour Assistance needed
July 30, 2006 04:31PM
(The Monologue continues ... but hopefully someone is helped by what I'm going through)

Ok. It's not the most elegant solution, but I've overloaded getStatement() with a version that is const. This method merely runs getStatement in its body, but since it is a const function, I can use it in the copy constructor.

Is there a better way to do this?
Re: 11th Hour Assistance needed
July 30, 2006 04:35PM
Did you read my posts? Aka, you must not copy the implementation object like that!! Delete your copy constructor from your source code. There is a reason why your are struggling. It is because you are trying to do something you should not.

Your map must be:
std::map<std::string, Account_ptr> nameList;
or something like that, depending on what exactly you are trying to do.
Re: 11th Hour Assistance needed
July 30, 2006 05:19PM
Sorry, Gustav. Did not see your post. I'll go off and change this now.
Re: 11th Hour Assistance needed
July 30, 2006 05:36PM
Gustav, thanks very much. It works. You have acted as a deus ex machina in a time of distress. Bless you.
Re: 11th Hour Assistance needed
July 30, 2006 05:54PM
Pleasure. Glad you have it sorted.
Re: 11th Hour Assistance needed
July 30, 2006 06:00PM
Gustav, another problem now related.

I store the Account_impl objects in the map, right? But now, I need to return a pointer to an Account (not an Account_impl) in the open() method of the Bank. How do I return it? I get an Account_impl pointer from the map. I can't just cast the thing (yes, I tried, it crashes the server).
Re: 11th Hour Assistance needed
July 30, 2006 06:45PM
Gustav, I've found the solution. Running the _this() method of the Account_impl object returns an Account pointer. So that works, but now I have a memory reference error.

I've put in debug traces all over the program. And I've figured out that the balance is incremented once, the Transaction object successfully created. The Statement object has its length increased by 1 to make space for the Transaction, and that the Transaction is stored in the statement.

Yet, I get the following error when I run the tcl client:

Instruction at 0x7c911f52 referenced memory at 0xfffffffd. The memory could not be read.

and the server crashes. what is wrong? Since the deposit code executed fine (I know this because the last line of code in it is the debug code saying the transaction was put into the statement).

TCL registers the deposit as failed.
Re: 11th Hour Assistance needed
July 30, 2006 06:55PM
Ok. I have found the problem. When I created an Account_impl object, I do not explicitly initialize the Statement object. It seems to start with 40000 odd members ... this caused the memory fault. That's fixed, and now tests 1 to 6 pass, but I'll save the chanting of Te Deums when test 11 passes.
Re: 11th Hour Assistance needed
July 30, 2006 08:27PM
C:\unisa\tcl>tclsh84 client.tcl
++++ ns-bound PASSED
++++ is_a_Bank PASSED
++++ open_acc PASSED
++++ deposit PASSED
++++ withdraw PASSED
++++ bankrupt PASSED
++++ statement PASSED
++++ auth PASSED
++++ dup PASSED
++++ remove PASSED
++++ stress PASSED
client.tcl: Total 11 Passed 11 Skipped 0 Failed 0

=========================================================================

C:\unisa\mico\proj>runserver

C:\unisa\mico\proj>server -ORBInitRef NameService=corbaloc::127.0.0.1:5555/NameS
ervice
Binding name in Naming Service ...
Bank server is up and running ...
In open(), going to create map iterator now to find name in nameList
Iterator created.
Account not found ...Account created.
New Account created and inserted into lists.
Account pointer set as return value in open()
In deposit(). Current balance is 0
Amount added to balance, which is now 40
Transaction to be stored created.
Space made in statement which has 0 transactions...and filled with transaction.
Statement length now 1.
In deposit(). Current balance is 40
Amount added to balance, which is now 50
Transaction to be stored created.
Space made in statement which has 1 transactions...and filled with transaction.
Statement length now 2.
Retrieving balance... retrieved.
Attempting to subtract 20 from current balance 50 ... done
Balance is 30
Transaction to be stored created.
Space created in statement for transaction ... and filled with transaction.
Retrieving balance... retrieved.
Withdrawal of 50 more than balance 30. Creating exception ... created. Preparing
to throw.
Statement to be gotten created.
Length of statement (3) retrieved.

Statement is:

Trans[0]: deposit of 40
Trans[1]: deposit of 10
Trans[2]: withdrawal of 20
Now attempting to make retval copy of statement.
Initizialing return Statement ...done.
Preparing to read transaction 0 ... Copying transaction 0 of statement ...
Preparing to read transaction 1 ... Copying transaction 1 of statement ...
Preparing to read transaction 2 ... Copying transaction 2 of statement ...
All transactions copied to statement to be returned.
In open(), going to create map iterator now to find name in nameList
Iterator created.
Account found. Preparing to check password ...Iterator created and initialized f
or passwordList
Boob is not correct for account Bob.
In open(), going to create map iterator now to find name in nameList
Iterator created.
Account found. Preparing to check password ...Iterator created and initialized f
or passwordList
Password correct.
Account pointer ready to be returned.
Retrieving balance... retrieved.
Getting ready to destroy account ...Account statement deleted.
Object reference removed.
Object deactivated
Iterators for nameList and passwordList created ... and initialized.
Account found. Will now delete ... deleted.
In open(), going to create map iterator now to find name in nameList
Iterator created.
Account not found ...Account created.
New Account created and inserted into lists.
Account pointer set as return value in open()
Statement to be gotten created.
Length of statement (0) retrieved.

Statement is:
BLANK.

Now attempting to make retval copy of statement.
Initizialing return Statement ...done.
All transactions copied to statement to be returned.
Getting ready to destroy account ...Account statement deleted.
Object reference removed.
Object deactivated
Iterators for nameList and passwordList created ... and initialized.
Account found. Will now delete ... deleted.
In open(), going to create map iterator now to find name in nameList
Iterator created.
Account not found ...Account created.
New Account created and inserted into lists.
Account pointer set as return value in open()
Statement to be gotten created.
Length of statement (0) retrieved.

Statement is:
BLANK.

Now attempting to make retval copy of statement.
Initizialing return Statement ...done.
All transactions copied to statement to be returned.
Getting ready to destroy account ...Account statement deleted.
Object reference removed.
Object deactivated
Iterators for nameList and passwordList created ... and initialized.
Account found. Will now delete ... deleted.
In open(), going to create map iterator now to find name in nameList
Iterator created.
Account not found ...Account created.
New Account created and inserted into lists.
Account pointer set as return value in open()
Statement to be gotten created.
Length of statement (0) retrieved.

Statement is:
BLANK.

Now attempting to make retval copy of statement.
Initizialing return Statement ...done.
All transactions copied to statement to be returned.
Getting ready to destroy account ...Account statement deleted.
Object reference removed.
Object deactivated
Iterators for nameList and passwordList created ... and initialized.
Account found. Will now delete ... deleted.
In open(), going to create map iterator now to find name in nameList
Iterator created.
Account not found ...Account created.
New Account created and inserted into lists.
Account pointer set as return value in open()
Statement to be gotten created.
Length of statement (0) retrieved.

Statement is:
BLANK.

Now attempting to make retval copy of statement.
Initizialing return Statement ...done.
All transactions copied to statement to be returned.
Getting ready to destroy account ...Account statement deleted.
Object reference removed.
Object deactivated
Iterators for nameList and passwordList created ... and initialized.
Account found. Will now delete ... deleted.
In open(), going to create map iterator now to find name in nameList
Iterator created.
Account not found ...Account created.
New Account created and inserted into lists.
Account pointer set as return value in open()
Statement to be gotten created.
Length of statement (0) retrieved.

Statement is:
BLANK.

Now attempting to make retval copy of statement.
Initizialing return Statement ...done.
All transactions copied to statement to be returned.
Getting ready to destroy account ...Account statement deleted.
Object reference removed.
Object deactivated
Iterators for nameList and passwordList created ... and initialized.
Account found. Will now delete ... deleted.
In open(), going to create map iterator now to find name in nameList
Iterator created.
Account not found ...Account created.
New Account created and inserted into lists.
Account pointer set as return value in open()
Statement to be gotten created.
Length of statement (0) retrieved.

Statement is:
BLANK.

Now attempting to make retval copy of statement.
Initizialing return Statement ...done.
All transactions copied to statement to be returned.
Getting ready to destroy account ...Account statement deleted.
Object reference removed.
Object deactivated
Iterators for nameList and passwordList created ... and initialized.
Account found. Will now delete ... deleted.
In open(), going to create map iterator now to find name in nameList
Iterator created.
Account not found ...Account created.
New Account created and inserted into lists.
Account pointer set as return value in open()
Statement to be gotten created.
Length of statement (0) retrieved.

Statement is:
BLANK.

Now attempting to make retval copy of statement.
Initizialing return Statement ...done.
All transactions copied to statement to be returned.
Getting ready to destroy account ...Account statement deleted.
Object reference removed.
Object deactivated
Iterators for nameList and passwordList created ... and initialized.
Account found. Will now delete ... deleted.
In open(), going to create map iterator now to find name in nameList
Iterator created.
Account not found ...Account created.
New Account created and inserted into lists.
Account pointer set as return value in open()
Statement to be gotten created.
Length of statement (0) retrieved.

Statement is:
BLANK.

Now attempting to make retval copy of statement.
Initizialing return Statement ...done.
All transactions copied to statement to be returned.
Getting ready to destroy account ...Account statement deleted.
Object reference removed.
Object deactivated
Iterators for nameList and passwordList created ... and initialized.
Account found. Will now delete ... deleted.
In open(), going to create map iterator now to find name in nameList
Iterator created.
Account not found ...Account created.
New Account created and inserted into lists.
Account pointer set as return value in open()
Statement to be gotten created.
Length of statement (0) retrieved.

Statement is:
BLANK.

Now attempting to make retval copy of statement.
Initizialing return Statement ...done.
All transactions copied to statement to be returned.
Getting ready to destroy account ...Account statement deleted.
Object reference removed.
Object deactivated
Iterators for nameList and passwordList created ... and initialized.
Account found. Will now delete ... deleted.
In open(), going to create map iterator now to find name in nameList
Iterator created.
Account not found ...Account created.
New Account created and inserted into lists.
Account pointer set as return value in open()
Statement to be gotten created.
Length of statement (0) retrieved.

Statement is:
BLANK.

Now attempting to make retval copy of statement.
Initizialing return Statement ...done.
All transactions copied to statement to be returned.
Getting ready to destroy account ...Account statement deleted.
Object reference removed.
Object deactivated
Iterators for nameList and passwordList created ... and initialized.
Account found. Will now delete ... deleted.
==============================================================================



Te Deum laudamus ...
Re: 11th Hour Assistance needed
July 31, 2006 01:31PM
Glad you got it sorted on your own steam. Well done.
Sorry, only registered users may post in this forum.

Click here to login