two main functions and you compile without a problem?Code:int main() { return 0; }
two main functions and you compile without a problem?Code:int main() { return 0; }
"All that we see or seem
Is but a dream within a dream." - Poe
Now you commented out the inclusion of the header file, from what I see from your image. By doing that, the definition of the BankAccount class will not be present in that source file (translation unit).Originally Posted by 5.5.7. Will
Oh, and don't post an image. Post your updated code in code tags; post your error message in full (in code tags too, if you will).
Your code in that file did not even get compiled (fully) because you had a header inclusion guard that did not belong there.Originally Posted by 5.5.7. Will
See above.Originally Posted by nimitzhunter
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
So post your updated code already. Just because you said that you did something doesn't mean you did it correctly.Originally Posted by 5.5.7. Will
EDIT:
And make sure that your updated code that you post is the one that results in the error message that you shall also post in full. No commenting out until that is what you compiled.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Code:#include <iostream>using namespace std; class BankAccount { private: double balance; // holds the current account balance double interestRate; // holds the interest rate for the current period double interest; // holds the interest earned for the current period int numTransactions; // holds the current number of transactions public: BankAccount(); void setInterestRate(double); void makeDeposit(double); bool makeWithdraw(double); void computeInterest(); double getInterestRate(); double getBalance(); double getInterest(); int getNumTransactions(); };Code:#include <iostream>#include "BankAccount.h" using namespace std; BankAccount::BankAccount() { balance = 0; interestRate = 0.056; interest = 0; numTransactions = 0; } void BankAccount::setInterestRate(double interestRate) { this->interestRate = interestRate; } void BankAccount::makeDeposit(double deposit) { balance += deposit; numTransactions++; } bool BankAccount::makeWithdraw(double withdraw) { if (balance < withdraw) return false; else { balance -= withdraw; numTransactions++; return true; } } void BankAccount::computeInterest() { interest = interestRate * balance; balance += interest; } double BankAccount::getInterestRate() { return interestRate; } double BankAccount::getBalance() { return balance; } double BankAccount::getInterest() { return interest; } int BankAccount::getNumTransactions() { return numTransactions; }------ Build started: Project: BankAccount.h, Configuration: Debug Win32 ------Code:#include <iostream>#include "BankAccount.h" using namespace std; void heading(); void displayMenu(); void makeDeposit(BankAccount &depositAmount); void makeWithdraw(BankAccount &withdrawAmount); int main() { heading(); cout << endl; displayMenu(); BankAccount savings; int userChoice; cin >> userChoice; for (userChoice; userChoice > 6;) { cout << endl; cout << "There is no option " << userChoice << ". Please choose a task that is listed." << endl; cout << endl; displayMenu(); cin >> userChoice; } if (userChoice == 6) { cout << endl; cout << "Thank you. Please wait." << endl; cout << endl; cout << "Thank you for being a loyal customer!" << endl; cout << endl; } if (userChoice == 1) { BankAccount::makeDeposit(savings); } if (userChoice == 2) { BankAccount::makeWithdraw(savings); } if (userChoice == 3) { savings.getBalance(); } if (userChoice == 4) { savings.getNumTransactions(); } if (userChoice == 5) { savings.getInterest(); } } void heading() { cout << "/********************************************************************************************" << endl; cout << "* Course Name: CMPS 191 – Modular Programming and Algorithms Development II" << endl; cout << "* Project 2" << endl; cout << "* Section: 01" << endl; cout << "* Semester: Spring 2013" << endl; cout << "*" << endl; cout << "* Project Members:" << endl; cout << "* Leader: William R. Veal III" << endl; cout << "*" << endl; cout << "*" << endl; cout << "* Instructor: Dr. Mathieu Kourouma" << endl; cout << "*" << endl; cout << "* Description:" << endl; cout << "* Provide here a brief description of the purpose of the project" << endl; cout << "*" << endl; cout << "* Due Date: Wednesday, March 26, 2013 by 11:59 PM on Blackboard" << endl; cout << "*" << endl; cout << "* Certificate of Authentication:" << endl; cout << "*" << endl; cout << "* We, the listed group members:" << endl; cout << "* Claim that this project’s work fully our own work." << endl; cout << "* We wrote this program independently." << endl; cout << "* We did not copy any part of the code from other group members." << endl; cout << "* We did not share at all our codes with other group members." << endl; cout << "* We are only allowed to seek help from the instructor of this course and group members" << endl; cout << "*" << endl; cout << "* Signature: WRVIII" << endl; cout << "*" << endl; cout << "**********************************************************************************************/" << endl; } void displayMenu() { cout << "----------------------------------------------------------------------------------------------------------------------------------" << endl; cout << " Welcome to SUBR's Credit Union Banking System" << endl; cout << "-----------------------------------------------------------------------------------------------------------------------------------" << endl; cout << "" << endl; cout << "Please select from the following menu:" << endl; cout << " " << endl; cout << "1. Make a deposit" << endl; cout << "2. Make a withdraw" << endl; cout << "3. Display account balance" << endl; cout << "4. Display the number of transactions" << endl; cout << "5. Display interest earned " << endl; cout << "6. Exit the banking system" << endl; cout << " " << endl; cout << "Make your selection: "; } void makeDeposit(BankAccount &depositAmount) { double deposit; cout << "Enter the amount you wish to deposit: "; cin >> deposit; cout << endl; depositAmount.makeDeposit(deposit); cout << "Your deposit is " << deposit << endl; } void makeWithdraw(BankAccount &withdrawAmount) { double withdraw; cout << "Enter the amount you wish to withdraw: "; cin >> withdraw; if (!withdrawAmount.makeWithdraw(withdraw)) cout << "The amount you want to withraw is larger than the balance." << endl; }
Compiling...
TestBankAccount.cpp
c:\users\william\documents\visual studio 2008\projects\bankaccount.h\bankaccount.h\testbank account.cpp(38) : error C2228: left of '.getBalance' must have class/struct/union
Code:if (userChoice == 1) { BankAccount::makeDeposit(savings); } if (userChoice == 2) { BankAccount::makeWithdraw(savings); }
EDIT: you have the functions
No such thing as BankAccount::makeWithdraw(Bankaccount &)...... fixing these and those code compiles just fine with g++. Don't see any problem with getBalance.Code:void makeDeposit(BankAccount &depositAmount); void makeWithdraw(BankAccount &withdrawAmount);
"All that we see or seem
Is but a dream within a dream." - Poe
Ah. Notice that you use nothing from <iostream> to define your BankAccount class. Therefore, I expect your header to be like:
Notice that I use header inclusion guards because it is a header. Putting public before private is just a matter of common C++ style that you don't have to follow.Code:#ifndef BANKACCOUNT_H #define BANKACCOUNT_H class BankAccount { public: BankAccount(); void setInterestRate(double); void makeDeposit(double); bool makeWithdraw(double); void computeInterest(); double getInterestRate(); double getBalance(); double getInterest(); int getNumTransactions(); private: double balance; // holds the current account balance double interestRate; // holds the interest rate for the current period double interest; // holds the interest earned for the current period int numTransactions; // holds the current number of transactions }; #endif
Now, concerning this:
Break it up into:Code:#include <iostream>#include "BankAccount.h"
for both source files. Compile and see what you get (i.e., you should get other error messages).Code:#include <iostream> #include "BankAccount.h"
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
"All that we see or seem
Is but a dream within a dream." - Poe
Code:#ifndef BANKACCOUNT_H #define BANKACCOUNT_H class BankAccount { public: BankAccount(); void setInterestRate(double); void makeDeposit(double); bool makeWithdraw(double); void computeInterest(); double getInterestRate(); double getBalance(); double getInterest(); int getNumTransactions(); private: double balance; // holds the current account balance double interestRate; // holds the interest rate for the current period double interest; // holds the interest earned for the current period int numTransactions; // holds the current number of transactions }; #endifCode:#include <iostream> #include "BankAccount.h" using namespace std; BankAccount::BankAccount() { balance = 0; interestRate = 0.056; interest = 0; numTransactions = 0; } void BankAccount::setInterestRate(double interestRate) { this->interestRate = interestRate; } void BankAccount::makeDeposit(double deposit) { balance += deposit; numTransactions++; } bool BankAccount::makeWithdraw(double withdraw) { if (balance < withdraw) return false; else { balance -= withdraw; numTransactions++; return true; } } void BankAccount::computeInterest() { interest = interestRate * balance; balance += interest; } double BankAccount::getInterestRate() { return interestRate; } double BankAccount::getBalance() { return balance; } double BankAccount::getInterest() { return interest; } int BankAccount::getNumTransactions() { return numTransactions; }i got the same exact error.Code:#include <iostream> #include "BankAccount.h" using namespace std; void heading(); void displayMenu(); void makeDeposit(BankAccount &depositAmount); void makeWithdraw(BankAccount &withdrawAmount); int main() { heading(); cout << endl; displayMenu(); BankAccount savings; int userChoice; cin >> userChoice; for (userChoice; userChoice > 6;) { cout << endl; cout << "There is no option " << userChoice << ". Please choose a task that is listed." << endl; cout << endl; displayMenu(); cin >> userChoice; } if (userChoice == 6) { cout << endl; cout << "Thank you. Please wait." << endl; cout << endl; cout << "Thank you for being a loyal customer!" << endl; cout << endl; } if (userChoice == 1) { BankAccount::makeDeposit(savings); } if (userChoice == 2) { BankAccount::makeWithdraw(savings); } if (userChoice == 3) { savings.getBalance(); } if (userChoice == 4) { savings.getNumTransactions(); } if (userChoice == 5) { savings.getInterest(); } } void heading() { cout << "/********************************************************************************************" << endl; cout << "* Course Name: CMPS 191 – Modular Programming and Algorithms Development II" << endl; cout << "* Project 2" << endl; cout << "* Section: 01" << endl; cout << "* Semester: Spring 2013" << endl; cout << "*" << endl; cout << "* Project Members:" << endl; cout << "* Leader: William R. Veal III" << endl; cout << "*" << endl; cout << "*" << endl; cout << "* Instructor: Dr. Mathieu Kourouma" << endl; cout << "*" << endl; cout << "* Description:" << endl; cout << "* Provide here a brief description of the purpose of the project" << endl; cout << "*" << endl; cout << "* Due Date: Wednesday, March 26, 2013 by 11:59 PM on Blackboard" << endl; cout << "*" << endl; cout << "* Certificate of Authentication:" << endl; cout << "*" << endl; cout << "* We, the listed group members:" << endl; cout << "* Claim that this project’s work fully our own work." << endl; cout << "* We wrote this program independently." << endl; cout << "* We did not copy any part of the code from other group members." << endl; cout << "* We did not share at all our codes with other group members." << endl; cout << "* We are only allowed to seek help from the instructor of this course and group members" << endl; cout << "*" << endl; cout << "* Signature: WRVIII" << endl; cout << "*" << endl; cout << "**********************************************************************************************/" << endl; } void displayMenu() { cout << "----------------------------------------------------------------------------------------------------------------------------------" << endl; cout << " Welcome to SUBR's Credit Union Banking System" << endl; cout << "-----------------------------------------------------------------------------------------------------------------------------------" << endl; cout << "" << endl; cout << "Please select from the following menu:" << endl; cout << " " << endl; cout << "1. Make a deposit" << endl; cout << "2. Make a withdraw" << endl; cout << "3. Display account balance" << endl; cout << "4. Display the number of transactions" << endl; cout << "5. Display interest earned " << endl; cout << "6. Exit the banking system" << endl; cout << " " << endl; cout << "Make your selection: "; } void makeDeposit(BankAccount &depositAmount) { double deposit; cout << "Enter the amount you wish to deposit: "; cin >> deposit; cout << endl; depositAmount.makeDeposit(deposit); cout << "Your deposit is " << deposit << endl; } void makeWithdraw(BankAccount &withdrawAmount) { double withdraw; cout << "Enter the amount you wish to withdraw: "; cin >> withdraw; if (!withdrawAmount.makeWithdraw(withdraw)) cout << "The amount you want to withraw is larger than the balance." << endl; }
can you change these function calls first?Code:if (userChoice == 1) { BankAccount::makeDeposit(savings); } if (userChoice == 2) { BankAccount::makeWithdraw(savings); }
"All that we see or seem
Is but a dream within a dream." - Poe
You still have the two include directives on the same line. The problem is that this:Originally Posted by 5.5.7. Will
could be interpreted as:Code:#include <iostream>#include "BankAccount.h"
with some rubbish characters after that. Therefore, you are not actually including BankAccount.h. If you compile at a higher warning level, your compiler might warn you about this. To fix this, it must be:Code:#include <iostream>
EDIT:Code:#include <iostream> #include "BankAccount.h"
You seem to have the same problem here:
is there something wrong that you keep writing these preprocessor directives on the same line?Code:#ifndef BANKACCOUNT_H#define BANKACCOUNT_H
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Likely like I did; use a good compiler fix the warnings and errors.
I also did most of the things that laserlight suggested.Code:if (userChoice == 1) { /* BankAccount:: */ makeDeposit(savings); } if (userChoice == 2) { /* BankAccount:: */ makeWithdraw(savings); }
Tim S.
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson