| | |
Have problem using size_t type...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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.
C++ Syntax (Toggle Plain Text)
#include<cstdlib> #include<string> #include<time.h> #include"Person.h" #include<iostream> using namespace std; typedef Person* PersonPtr; class BankAccount{ public: //friend ostream& operator << (ostream& aOutput, const BankAccount& aBankAccount); //friend istream& operator >> (istream& aInput, BankAccount& aBankAccount); //friend bool operator == (const BankAccount& aBankAccount1, const BankAccount& aBankAccount2); BankAccount(); BankAccount(const Person& aPerson); /** double withdrawal(double aAmount); double deposit(double aAmount);*/ size_t getAccountNumber() const; /**void setBalance(double aBalance); double getBalance() const; void setAccountOwner(const Person& aPerson); Person getAccountOwner() const; bool sufficientFundsToWithdrawal(double aAmount) const;*/ private: PersonPtr mPerson; //mAmount; double mBalance; [COLOR="red"]size_t mAccountNumber;[/COLOR] }; //#endif int main() { //BankAccount ba; Person p; cout <<"running"; getAccountNumber(); //cin >> ba; //cout << ba; return 0; } /** ostream& operator << (ostream& aOutput, const BankAccount& aBankAccount){ cout << "A new account with account number"; aOutput << aBankAccount.mAccountNumber << endl; cout << "has been created for"; aOutput << aBankAccount.mPerson << endl; return aOutput; } istream& operator >> (istream &aInput, BankAccount& aBankAccount){ cout << "Please type the intitial balance for account number: " << aBankAccount.mAccountNumber << endl; aInput >> aBankAccount.mBalance; aInput >> aBank.mPerson; cout << "type the first name\n; aInput >> aBankAccount.mFName; cout << "type the last name\n; aInput >> aBankAccount.mLName; cout << "Type the address\n"; aInput >> aBankAccount.mAddress; cout << "type the name of the state\n"; aInput >> aBankAccount.mState; cout << "type the name of the city\n"; aInput >> aBankAccount.mCity; cout << "type the zip code\n"; aInput >> aBankAccount.mZip; return aInput; } bool operator == (const BankAccount& aBankAccount1, const BankAccount& aBankAccount2){ return(aBankAccount1.mPerson == aBankAccount2.mPerson && aBankAccount1.mBalance == aBankAccount2.mBalance && aBankAccount1.mAccountNumber == aBankAccount2.mAccountNumber); } BankAccount::BankAccount(){ mPerson = new Person; mBalance = 5.00; mAccountNumber = getAccountNumber(); } BankAccount::BankAccount(const Person &aPerson):mPerson( new Person), mBalance(5.00),mAccountNumber(getAccountNumber()){ } void setAccountOwner (const Person& aPerson){ mPerson = new Person; } Person getAccountOwner () const{ return mPerson; } double withdrawal (double aAmount){ double newBalance = Balance - aAmount; setBalance(newBalance); getBalance(); //return aBalance; } double deposit (double aAmount){ double newBalance = newBalance + aAmount; aBankAccount.setBalance(newBalance); aBankAccount.getBalance (); }*/ size_t getAccountNumber () const { srand(static_cast<unsigned int>(time(NULL))); mAccountNumber = rand(); return mAccountNumber; }/** void setBalance (double aBalance) { mBalance = aBalance; } double getBalance () const{ return mBalance; } bool sufficientFundsToWithdrawal (double aAmount) const { if((aAmount - aBalance) > 5){ return true; } else { return false; } } */
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();
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
•
•
•
•
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)
#include<cstdlib> #include<string> #include<time.h> #include"Person.h" #include<iostream> using namespace std; typedef Person* PersonPtr; class BankAccount{ public: //friend ostream& operator << (ostream& aOutput, const BankAccount& aBankAccount); //friend istream& operator >> (istream& aInput, BankAccount& aBankAccount); //friend bool operator == (const BankAccount& aBankAccount1, const BankAccount& aBankAccount2); BankAccount(); BankAccount(const Person& aPerson); /** double withdrawal(double aAmount); double deposit(double aAmount);*/ size_t getAccountNumber() const; /**void setBalance(double aBalance); double getBalance() const; void setAccountOwner(const Person& aPerson); Person getAccountOwner() const; bool sufficientFundsToWithdrawal(double aAmount) const;*/ private: PersonPtr mPerson; //mAmount; double mBalance; [COLOR="red"]size_t mAccountNumber;[/COLOR] }; //#endif int main() { //BankAccount ba; Person p; cout <<"running"; getAccountNumber; //cin >> ba; //cout << ba; return 0; } /** ostream& operator << (ostream& aOutput, const BankAccount& aBankAccount){ cout << "A new account with account number"; aOutput << aBankAccount.mAccountNumber << endl; cout << "has been created for"; aOutput << aBankAccount.mPerson << endl; return aOutput; } istream& operator >> (istream &aInput, BankAccount& aBankAccount){ cout << "Please type the intitial balance for account number: " << aBankAccount.mAccountNumber << endl; aInput >> aBankAccount.mBalance; aInput >> aBank.mPerson; cout << "type the first name\n; aInput >> aBankAccount.mFName; cout << "type the last name\n; aInput >> aBankAccount.mLName; cout << "Type the address\n"; aInput >> aBankAccount.mAddress; cout << "type the name of the state\n"; aInput >> aBankAccount.mState; cout << "type the name of the city\n"; aInput >> aBankAccount.mCity; cout << "type the zip code\n"; aInput >> aBankAccount.mZip; return aInput; } bool operator == (const BankAccount& aBankAccount1, const BankAccount& aBankAccount2){ return(aBankAccount1.mPerson == aBankAccount2.mPerson && aBankAccount1.mBalance == aBankAccount2.mBalance && aBankAccount1.mAccountNumber == aBankAccount2.mAccountNumber); } BankAccount::BankAccount(){ mPerson = new Person; mBalance = 5.00; mAccountNumber = getAccountNumber(); } BankAccount::BankAccount(const Person &aPerson):mPerson( new Person), mBalance(5.00),mAccountNumber(getAccountNumber()){ } void setAccountOwner (const Person& aPerson){ mPerson = new Person; } Person getAccountOwner () const{ return mPerson; } double withdrawal (double aAmount){ double newBalance = Balance - aAmount; setBalance(newBalance); getBalance(); //return aBalance; } double deposit (double aAmount){ double newBalance = newBalance + aAmount; aBankAccount.setBalance(newBalance); aBankAccount.getBalance (); }*/ size_t getAccountNumber () const { srand(static_cast<unsigned int>(time(NULL))); mAccountNumber = rand(); return mAccountNumber; }/** void setBalance (double aBalance) { mBalance = aBalance; } double getBalance () const{ return mBalance; } bool sufficientFundsToWithdrawal (double aAmount) const { if((aAmount - aBalance) > 5){ return true; } else { return false; } } */
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.
C++ Syntax (Toggle Plain Text)
size_t BankAccount::getAccountNumber () const{
Line 36: This isn't a function call. Try changing it to something line this:
C++ Syntax (Toggle Plain Text)
size_t acctNum = p.getAccountNumber ();
Last edited by VernonDozier; May 11th, 2008 at 3:46 pm. Reason: fixed typo
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
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)
size_t BankAccount::getAccountNumber () { // set the value here ... }
C++ Syntax (Toggle Plain Text)
#include<cstdlib> #include<string> #include<time.h> #include"Person.h" #include<iostream> using namespace std; typedef Person* PersonPtr; class BankAccount{ public: //friend ostream& operator << (ostream& aOutput, const BankAccount& aBankAccount); //friend istream& operator >> (istream& aInput, BankAccount& aBankAccount); //friend bool operator == (const BankAccount& aBankAccount1, const BankAccount& aBankAccount2); BankAccount(); BankAccount(const Person& aPerson); /** double withdrawal(double aAmount); double deposit(double aAmount);*/ size_t getAccountNumber() const; /**void setBalance(double aBalance); double getBalance() const; void setAccountOwner(const Person& aPerson); Person getAccountOwner() const; bool sufficientFundsToWithdrawal(double aAmount) const;*/ private: PersonPtr mPerson; //mAmount; double mBalance; size_t mAccountNumber; }; //#endif int main() { BankAccount ba; //Person p; cout <<"running"; ba.getAccountNumber(); //cin >> ba; //cout << ba; return 0; } /** ostream& operator << (ostream& aOutput, const BankAccount& aBankAccount){ cout << "A new account with account number"; aOutput << aBankAccount.mAccountNumber << endl; cout << "has been created for"; aOutput << aBankAccount.mPerson << endl; return aOutput; } istream& operator >> (istream &aInput, BankAccount& aBankAccount){ cout << "Please type the intitial balance for account number: " << aBankAccount.mAccountNumber << endl; aInput >> aBankAccount.mBalance; aInput >> aBank.mPerson; cout << "type the first name\n; aInput >> aBankAccount.mFName; cout << "type the last name\n; aInput >> aBankAccount.mLName; cout << "Type the address\n"; aInput >> aBankAccount.mAddress; cout << "type the name of the state\n"; aInput >> aBankAccount.mState; cout << "type the name of the city\n"; aInput >> aBankAccount.mCity; cout << "type the zip code\n"; aInput >> aBankAccount.mZip; return aInput; } bool operator == (const BankAccount& aBankAccount1, const BankAccount& aBankAccount2){ return(aBankAccount1.mPerson == aBankAccount2.mPerson && aBankAccount1.mBalance == aBankAccount2.mBalance && aBankAccount1.mAccountNumber == aBankAccount2.mAccountNumber); } BankAccount::BankAccount(){ mPerson = new Person; mBalance = 5.00; mAccountNumber = getAccountNumber(); } BankAccount::BankAccount(const Person &aPerson):mPerson( new Person), mBalance(5.00),mAccountNumber(getAccountNumber()){ } void setAccountOwner (const Person& aPerson){ mPerson = new Person; } Person getAccountOwner () const{ return mPerson; } double withdrawal (double aAmount){ double newBalance = Balance - aAmount; setBalance(newBalance); getBalance(); //return aBalance; } double deposit (double aAmount){ double newBalance = newBalance + aAmount; aBankAccount.setBalance(newBalance); aBankAccount.getBalance (); }*/ size_t BankAccount::getAccountNumber () const{ srand(static_cast<unsigned int>(time(NULL))); mAccountNumber = rand(); return mAccountNumber; }/** void setBalance (double aBalance) { mBalance = aBalance; } double getBalance () const{ return mBalance; } bool sufficientFundsToWithdrawal (double aAmount) const { if((aAmount - aBalance) > 5){ return true; } else { return false; } } */
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 ?
In my code the line 107 is
so it's complainig about this line
C++ Syntax (Toggle Plain Text)
mAccountNumber = rand();
so it's complainig about this line
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
Whoops, gave some bad advice. p is type Person, not BankAccount so this won't work:
You'll want to create a variable of type BankAccount:
Also, see mitrmkar's post and remove the const modifier.
C++ Syntax (Toggle Plain Text)
p.getAccountNumber ()
C++ Syntax (Toggle Plain Text)
BankAccount ba = new BankAccount (); ba.getAccountNumber ();
Last edited by VernonDozier; May 11th, 2008 at 4:10 pm. Reason: cleaned up language to remove confusion and changed code line
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 but not clear why you have allocated dynamic memory to bankAccount here in
do we really need to do this, It won't work without doing this?
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)
ba.getAccountNumber();
C++ Syntax (Toggle Plain Text)
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
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
•
•
•
•
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 likebut not clear why you have allocated dynamic memory to bankAccount here inC++ Syntax (Toggle Plain Text)
ba.getAccountNumber();
C++ Syntax (Toggle Plain Text)
BankAccount ba = new BankAccount ();
do we really need to do this, It won't work without doing this?
C++ Syntax (Toggle Plain Text)
BankAccount ba;
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.
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
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.
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.
![]() |
Similar Threads
- problem with size_t filesize (C)
- simple string assignment problem (C)
- Problem (C++)
- help vector problem (C++)
- problem instantiating a subclass of abstract class (C)
- Problem with memory allocation =( (C)
- can't seem to make a yes/no question (C)
- Problem of sorting words of each string using pointers (C++)
- IMF message filter SDK sample problem (C)
Other Threads in the C++ Forum
- Previous Thread: Delete Rows in dataGridView
- Next Thread: help with text editing
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






