943,618 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2945
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 11th, 2008
0

Have problem using size_t type...

Expand Post »
Here is my code for the program. which I'm trying to complie...I just started testing it and was going through it step by step...but the first function whichI started testing is getAccountNumbers, but there are some issues with it, here's the code, it has Person file included in it, it just takes the detais of the person.

C++ Syntax (Toggle Plain Text)
  1. #include<cstdlib>
  2. #include<string>
  3. #include<time.h>
  4. #include"Person.h"
  5. #include<iostream>
  6. using namespace std;
  7.  
  8. typedef Person* PersonPtr;
  9. class BankAccount{
  10. public:
  11. //friend ostream& operator << (ostream& aOutput, const BankAccount& aBankAccount);
  12. //friend istream& operator >> (istream& aInput, BankAccount& aBankAccount);
  13. //friend bool operator == (const BankAccount& aBankAccount1, const BankAccount& aBankAccount2);
  14. BankAccount();
  15. BankAccount(const Person& aPerson);
  16. /** double withdrawal(double aAmount);
  17. double deposit(double aAmount);*/
  18. size_t getAccountNumber() const;
  19. /**void setBalance(double aBalance);
  20. double getBalance() const;
  21. void setAccountOwner(const Person& aPerson);
  22. Person getAccountOwner() const;
  23. bool sufficientFundsToWithdrawal(double aAmount) const;*/
  24. private:
  25. PersonPtr mPerson;
  26. //mAmount;
  27. double mBalance;
  28. [COLOR="red"]size_t mAccountNumber;[/COLOR]
  29. };
  30. //#endif
  31.  
  32. int main() {
  33. //BankAccount ba;
  34. Person p;
  35. cout <<"running";
  36. getAccountNumber();
  37. //cin >> ba;
  38. //cout << ba;
  39.  
  40. return 0;
  41. }
  42.  
  43. /**
  44. ostream& operator << (ostream& aOutput, const BankAccount& aBankAccount){
  45. cout << "A new account with account number";
  46. aOutput << aBankAccount.mAccountNumber << endl;
  47. cout << "has been created for";
  48. aOutput << aBankAccount.mPerson << endl;
  49. return aOutput;
  50. }
  51. istream& operator >> (istream &aInput, BankAccount& aBankAccount){
  52. cout << "Please type the intitial balance for account number: " << aBankAccount.mAccountNumber << endl;
  53. aInput >> aBankAccount.mBalance;
  54. aInput >> aBank.mPerson;
  55.  
  56. cout << "type the first name\n;
  57. aInput >> aBankAccount.mFName;
  58. cout << "type the last name\n;
  59. aInput >> aBankAccount.mLName;
  60. cout << "Type the address\n";
  61. aInput >> aBankAccount.mAddress;
  62. cout << "type the name of the state\n";
  63. aInput >> aBankAccount.mState;
  64. cout << "type the name of the city\n";
  65. aInput >> aBankAccount.mCity;
  66. cout << "type the zip code\n";
  67. aInput >> aBankAccount.mZip;
  68.  
  69. return aInput;
  70. }
  71. bool operator == (const BankAccount& aBankAccount1, const BankAccount& aBankAccount2){
  72. return(aBankAccount1.mPerson == aBankAccount2.mPerson &&
  73. aBankAccount1.mBalance == aBankAccount2.mBalance &&
  74. aBankAccount1.mAccountNumber == aBankAccount2.mAccountNumber);
  75. }
  76. BankAccount::BankAccount(){
  77. mPerson = new Person;
  78. mBalance = 5.00;
  79. mAccountNumber = getAccountNumber();
  80. }
  81.  
  82. BankAccount::BankAccount(const Person &aPerson):mPerson( new Person), mBalance(5.00),mAccountNumber(getAccountNumber()){
  83. }
  84. void setAccountOwner (const Person& aPerson){
  85. mPerson = new Person;
  86. }
  87. Person getAccountOwner () const{
  88. return mPerson;
  89. }
  90. double withdrawal (double aAmount){
  91. double newBalance = Balance - aAmount;
  92. setBalance(newBalance);
  93. getBalance();
  94. //return aBalance;
  95.  
  96. }
  97.  
  98. double deposit (double aAmount){
  99. double newBalance = newBalance + aAmount;
  100. aBankAccount.setBalance(newBalance);
  101. aBankAccount.getBalance ();
  102.  
  103. }*/
  104. size_t getAccountNumber () const {
  105. srand(static_cast<unsigned int>(time(NULL)));
  106. mAccountNumber = rand();
  107. return mAccountNumber;
  108. }/**
  109. void setBalance (double aBalance) {
  110. mBalance = aBalance;
  111. }
  112. double getBalance () const{
  113. return mBalance;
  114. }
  115. bool sufficientFundsToWithdrawal (double aAmount) const {
  116. if((aAmount - aBalance) > 5){
  117. return true;
  118. }
  119. else {
  120. return false;
  121. }
  122. }
  123. */

I'm trying to test only the getAccountNumber function here . But the compiler giives me error like this:

1>c:\users\sherry\documents\visual studio 2008\projects\testing\testing\bankaccount.cpp(37) : error C3861: 'getAccountNumber': identifier not found
1>c:\users\sherry\documents\visual studio 2008\projects\testing\testing\bankaccount.cpp(105) : error C2270: 'getAccountNumber' : modifiers not allowed on nonmember functions
1>c:\users\sherry\documents\visual studio 2008\projects\testing\testing\bankaccount.cpp(107) : error C2065: 'mAccountNumber' : undeclared identifier
1>c:\users\sherry\documents\visual studio 2008\projects\testing\testing\bankaccount.cpp(108) : error C2065: 'mAccountNumber' : undeclared identifier


Please help me figure out how to handle that size_t type an wy its complainig that mAccountNumber identifier is undeclared.
Last edited by code12; May 11th, 2008 at 3:40 pm. Reason: brackets "()"missing after function call getAccountNumber();
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
code12 is offline Offline
51 posts
since Mar 2008
May 11th, 2008
0

Re: Have problem using size_t type...

Click to Expand / Collapse  Quote originally posted by code12 ...
Here is my code for the program. which I'm trying to complie...I just started testing it and was going through it step by step...but the first function whichI started testing is getAccountNumbers, but there are some issues with it, here's the code, it has Person file included in it, it just takes the detais of the person.

C++ Syntax (Toggle Plain Text)
  1. #include<cstdlib>
  2. #include<string>
  3. #include<time.h>
  4. #include"Person.h"
  5. #include<iostream>
  6. using namespace std;
  7.  
  8. typedef Person* PersonPtr;
  9. class BankAccount{
  10. public:
  11. //friend ostream& operator << (ostream& aOutput, const BankAccount& aBankAccount);
  12. //friend istream& operator >> (istream& aInput, BankAccount& aBankAccount);
  13. //friend bool operator == (const BankAccount& aBankAccount1, const BankAccount& aBankAccount2);
  14. BankAccount();
  15. BankAccount(const Person& aPerson);
  16. /** double withdrawal(double aAmount);
  17. double deposit(double aAmount);*/
  18. size_t getAccountNumber() const;
  19. /**void setBalance(double aBalance);
  20. double getBalance() const;
  21. void setAccountOwner(const Person& aPerson);
  22. Person getAccountOwner() const;
  23. bool sufficientFundsToWithdrawal(double aAmount) const;*/
  24. private:
  25. PersonPtr mPerson;
  26. //mAmount;
  27. double mBalance;
  28. [COLOR="red"]size_t mAccountNumber;[/COLOR]
  29. };
  30. //#endif
  31.  
  32. int main() {
  33. //BankAccount ba;
  34. Person p;
  35. cout <<"running";
  36. getAccountNumber;
  37. //cin >> ba;
  38. //cout << ba;
  39.  
  40. return 0;
  41. }
  42.  
  43. /**
  44. ostream& operator << (ostream& aOutput, const BankAccount& aBankAccount){
  45. cout << "A new account with account number";
  46. aOutput << aBankAccount.mAccountNumber << endl;
  47. cout << "has been created for";
  48. aOutput << aBankAccount.mPerson << endl;
  49. return aOutput;
  50. }
  51. istream& operator >> (istream &aInput, BankAccount& aBankAccount){
  52. cout << "Please type the intitial balance for account number: " << aBankAccount.mAccountNumber << endl;
  53. aInput >> aBankAccount.mBalance;
  54. aInput >> aBank.mPerson;
  55.  
  56. cout << "type the first name\n;
  57. aInput >> aBankAccount.mFName;
  58. cout << "type the last name\n;
  59. aInput >> aBankAccount.mLName;
  60. cout << "Type the address\n";
  61. aInput >> aBankAccount.mAddress;
  62. cout << "type the name of the state\n";
  63. aInput >> aBankAccount.mState;
  64. cout << "type the name of the city\n";
  65. aInput >> aBankAccount.mCity;
  66. cout << "type the zip code\n";
  67. aInput >> aBankAccount.mZip;
  68.  
  69. return aInput;
  70. }
  71. bool operator == (const BankAccount& aBankAccount1, const BankAccount& aBankAccount2){
  72. return(aBankAccount1.mPerson == aBankAccount2.mPerson &&
  73. aBankAccount1.mBalance == aBankAccount2.mBalance &&
  74. aBankAccount1.mAccountNumber == aBankAccount2.mAccountNumber);
  75. }
  76. BankAccount::BankAccount(){
  77. mPerson = new Person;
  78. mBalance = 5.00;
  79. mAccountNumber = getAccountNumber();
  80. }
  81.  
  82. BankAccount::BankAccount(const Person &aPerson):mPerson( new Person), mBalance(5.00),mAccountNumber(getAccountNumber()){
  83. }
  84. void setAccountOwner (const Person& aPerson){
  85. mPerson = new Person;
  86. }
  87. Person getAccountOwner () const{
  88. return mPerson;
  89. }
  90. double withdrawal (double aAmount){
  91. double newBalance = Balance - aAmount;
  92. setBalance(newBalance);
  93. getBalance();
  94. //return aBalance;
  95.  
  96. }
  97.  
  98. double deposit (double aAmount){
  99. double newBalance = newBalance + aAmount;
  100. aBankAccount.setBalance(newBalance);
  101. aBankAccount.getBalance ();
  102.  
  103. }*/
  104. size_t getAccountNumber () const {
  105. srand(static_cast<unsigned int>(time(NULL)));
  106. mAccountNumber = rand();
  107. return mAccountNumber;
  108. }/**
  109. void setBalance (double aBalance) {
  110. mBalance = aBalance;
  111. }
  112. double getBalance () const{
  113. return mBalance;
  114. }
  115. bool sufficientFundsToWithdrawal (double aAmount) const {
  116. if((aAmount - aBalance) > 5){
  117. return true;
  118. }
  119. else {
  120. return false;
  121. }
  122. }
  123. */

I'm trying to test only the getAccountNumber function here . But the compiler giives me error like this:

1>c:\users\sherry\documents\visual studio 2008\projects\testing\testing\bankaccount.cpp(37) : error C3861: 'getAccountNumber': identifier not found
1>c:\users\sherry\documents\visual studio 2008\projects\testing\testing\bankaccount.cpp(105) : error C2270: 'getAccountNumber' : modifiers not allowed on nonmember functions
1>c:\users\sherry\documents\visual studio 2008\projects\testing\testing\bankaccount.cpp(107) : error C2065: 'mAccountNumber' : undeclared identifier
1>c:\users\sherry\documents\visual studio 2008\projects\testing\testing\bankaccount.cpp(108) : error C2065: 'mAccountNumber' : undeclared identifier


Please help me figure out how to handle that size_t type an wy its complainig that mAccountNumber identifier is undeclared.
size_t is not the problem here. The problem is the way you have set up your class and the function call. Change line 104 to this:
C++ Syntax (Toggle Plain Text)
  1. size_t BankAccount::getAccountNumber () const{
This tells the compiler that this function is part of the BankAccount class.

Line 36: This isn't a function call. Try changing it to something line this:
C++ Syntax (Toggle Plain Text)
  1. size_t acctNum = p.getAccountNumber ();
This is assuming that the getAccountNumber function you want to call is the one that is part of the BankAccount class.
Last edited by VernonDozier; May 11th, 2008 at 3:46 pm. Reason: fixed typo
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
May 11th, 2008
0

Re: Have problem using size_t type...

If you are going to set the value of mAccountNumber inside BankAccount::getAccountNumber(), then the function must be non-const, i.e. you need to have it like:

C++ Syntax (Toggle Plain Text)
  1. size_t BankAccount::getAccountNumber ()
  2. {
  3. // set the value here ...
  4. }
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
May 11th, 2008
0

Re: Have problem using size_t type...

C++ Syntax (Toggle Plain Text)
  1. #include<cstdlib>
  2. #include<string>
  3. #include<time.h>
  4. #include"Person.h"
  5. #include<iostream>
  6. using namespace std;
  7.  
  8. typedef Person* PersonPtr;
  9. class BankAccount{
  10. public:
  11. //friend ostream& operator << (ostream& aOutput, const BankAccount& aBankAccount);
  12. //friend istream& operator >> (istream& aInput, BankAccount& aBankAccount);
  13. //friend bool operator == (const BankAccount& aBankAccount1, const BankAccount& aBankAccount2);
  14. BankAccount();
  15. BankAccount(const Person& aPerson);
  16. /** double withdrawal(double aAmount);
  17. double deposit(double aAmount);*/
  18. size_t getAccountNumber() const;
  19. /**void setBalance(double aBalance);
  20. double getBalance() const;
  21. void setAccountOwner(const Person& aPerson);
  22. Person getAccountOwner() const;
  23. bool sufficientFundsToWithdrawal(double aAmount) const;*/
  24. private:
  25. PersonPtr mPerson;
  26. //mAmount;
  27. double mBalance;
  28. size_t mAccountNumber;
  29. };
  30. //#endif
  31.  
  32. int main() {
  33. BankAccount ba;
  34. //Person p;
  35. cout <<"running";
  36. ba.getAccountNumber();
  37. //cin >> ba;
  38. //cout << ba;
  39.  
  40. return 0;
  41. }
  42.  
  43. /**
  44. ostream& operator << (ostream& aOutput, const BankAccount& aBankAccount){
  45. cout << "A new account with account number";
  46. aOutput << aBankAccount.mAccountNumber << endl;
  47. cout << "has been created for";
  48. aOutput << aBankAccount.mPerson << endl;
  49. return aOutput;
  50. }
  51. istream& operator >> (istream &aInput, BankAccount& aBankAccount){
  52. cout << "Please type the intitial balance for account number: " << aBankAccount.mAccountNumber << endl;
  53. aInput >> aBankAccount.mBalance;
  54. aInput >> aBank.mPerson;
  55.  
  56. cout << "type the first name\n;
  57. aInput >> aBankAccount.mFName;
  58. cout << "type the last name\n;
  59. aInput >> aBankAccount.mLName;
  60. cout << "Type the address\n";
  61. aInput >> aBankAccount.mAddress;
  62. cout << "type the name of the state\n";
  63. aInput >> aBankAccount.mState;
  64. cout << "type the name of the city\n";
  65. aInput >> aBankAccount.mCity;
  66. cout << "type the zip code\n";
  67. aInput >> aBankAccount.mZip;
  68.  
  69. return aInput;
  70. }
  71. bool operator == (const BankAccount& aBankAccount1, const BankAccount& aBankAccount2){
  72. return(aBankAccount1.mPerson == aBankAccount2.mPerson &&
  73. aBankAccount1.mBalance == aBankAccount2.mBalance &&
  74. aBankAccount1.mAccountNumber == aBankAccount2.mAccountNumber);
  75. }
  76. BankAccount::BankAccount(){
  77. mPerson = new Person;
  78. mBalance = 5.00;
  79. mAccountNumber = getAccountNumber();
  80. }
  81.  
  82. BankAccount::BankAccount(const Person &aPerson):mPerson( new Person), mBalance(5.00),mAccountNumber(getAccountNumber()){
  83. }
  84. void setAccountOwner (const Person& aPerson){
  85. mPerson = new Person;
  86. }
  87. Person getAccountOwner () const{
  88. return mPerson;
  89. }
  90. double withdrawal (double aAmount){
  91. double newBalance = Balance - aAmount;
  92. setBalance(newBalance);
  93. getBalance();
  94. //return aBalance;
  95.  
  96. }
  97.  
  98. double deposit (double aAmount){
  99. double newBalance = newBalance + aAmount;
  100. aBankAccount.setBalance(newBalance);
  101. aBankAccount.getBalance ();
  102.  
  103. }*/
  104. size_t BankAccount::getAccountNumber () const{
  105. srand(static_cast<unsigned int>(time(NULL)));
  106. mAccountNumber = rand();
  107. return mAccountNumber;
  108. }/**
  109. void setBalance (double aBalance) {
  110. mBalance = aBalance;
  111. }
  112. double getBalance () const{
  113. return mBalance;
  114. }
  115. bool sufficientFundsToWithdrawal (double aAmount) const {
  116. if((aAmount - aBalance) > 5){
  117. return true;
  118. }
  119. else {
  120. return false;
  121. }
  122. }
  123. */

i edited the things which u told to do, and it has really solved those of the errors, but now a new error is showing up:
it is like this:
1>c:\users\sherry\documents\visual studio 2008\projects\testing\testing\bankaccount.cpp(107) : error C2166: l-value specifies const object


what is this l-value ?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
code12 is offline Offline
51 posts
since Mar 2008
May 11th, 2008
0

Re: Have problem using size_t type...

In my code the line 107 is
C++ Syntax (Toggle Plain Text)
  1. mAccountNumber = rand();

so it's complainig about this line
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
code12 is offline Offline
51 posts
since Mar 2008
May 11th, 2008
0

Re: Have problem using size_t type...

Whoops, gave some bad advice. p is type Person, not BankAccount so this won't work:

C++ Syntax (Toggle Plain Text)
  1. p.getAccountNumber ()
You'll want to create a variable of type BankAccount:
C++ Syntax (Toggle Plain Text)
  1. BankAccount ba = new BankAccount ();
  2. ba.getAccountNumber ();
Also, see mitrmkar's post and remove the const modifier.
Last edited by VernonDozier; May 11th, 2008 at 4:10 pm. Reason: cleaned up language to remove confusion and changed code line
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
May 11th, 2008
0

Re: Have problem using size_t type...

you know I'm not using any set function for creating a bank account, so once this value is returned in getBankAccountNumber, it won't be changed after that. this way every person would have a unique accountnumber.

so I have to declare it as a const(means we won't be changing it's value once it's created)

regarding the variable for BankAccount I did create a variable for BankAccount as
BankAccount ba
& then I did like
C++ Syntax (Toggle Plain Text)
  1. ba.getAccountNumber();
but not clear why you have allocated dynamic memory to bankAccount here in
C++ Syntax (Toggle Plain Text)
  1. BankAccount ba = new BankAccount ();

do we really need to do this, It won't work without doing this?
Last edited by code12; May 11th, 2008 at 4:17 pm. Reason: FORMATTED
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
code12 is offline Offline
51 posts
since Mar 2008
May 11th, 2008
0

Re: Have problem using size_t type...

MITRMKAR do you agree with what i wrote in my last post regarding use of const.
i guess this should work?

If no then . i think I will need to include set function even and have the chances of changing the value of bankAccount, which was created before creating any bank account.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
code12 is offline Offline
51 posts
since Mar 2008
May 11th, 2008
0

Re: Have problem using size_t type...

Click to Expand / Collapse  Quote originally posted by code12 ...
you know I'm not using any set function for creating a bank account, so once this value is returned in getBankAccountNumber, it won't be changed after that. this way every person would have a unique accountnumber.

so I have to declare it as a const(means we won't be changing it's value once it's created)

regarding the variable for BankAccount I did create a variable for BankAccount as
BankAccount ba
& then I did like
C++ Syntax (Toggle Plain Text)
  1. ba.getAccountNumber();
but not clear why you have allocated dynamic memory to bankAccount here in
C++ Syntax (Toggle Plain Text)
  1. BankAccount ba = new BankAccount ();

do we really need to do this, It won't work without doing this?
I hadn't noticed your updated code when I posted. Dynamic versus non-dynamic shouldn't matter:
C++ Syntax (Toggle Plain Text)
  1. BankAccount ba;
should work fine.



Regarding the const modifier, if it was me, I'd have a set function. Seems like the easiest way to do it. If you want to do it with the get function and not have a set function, I can't think of a way to do it with the const modifier. Maybe there is one, but even if there is a way, I'd have a set function anyway since I think it's more descriptive. Anyway, that's just my opinion. I'd have a setAccountNumber () function which you call once and have that function not have a const modifier, then have a getAccountNumber () and put the const modifier on that function. Have the bank account number be set using rand in the setAccountNumber and then just have the getAccountNumber function return the value and put the const modifier on it. That's how I'd do it.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
May 11th, 2008
0

Re: Have problem using size_t type...

Hmm .. if you have a member function of a class declared as const, then you are not to modify the class instance's data. If you can cope with it, then fine. (I'm not fully following with what you wrote, sorry).

You might have a generateAccountNumber() which returns the generated new value that you can assign to a newly created account object and leave the const getBankAccountNumber() for just returning the pre-set account number.
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Delete Rows in dataGridView
Next Thread in C++ Forum Timeline: help with text editing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC