I started learning cpp programming officially yesterday. Here's my first cpp program which is fraught with problems. It consists of 3 files and emulates a bank ATM. It is an exercise from Deitel's book "C++ how to program".
I use Eclipse+Cygwin (both the latest version) on a Win 8 PC. The compiler appeared to have problems with account.displayAcctBalance() in the main.cpp. Whenever it is called (line 24, 26, 33, and 35), an error pops up with a msg like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Multiple markers at this line
- cannot convert 'account.BankAccount::displayAcctBalance()' (type 'void') to type 'const signed
char*'
- cannot convert 'account.BankAccount::displayAcctBalance()' (type 'void') to type 'const char*'
- mismatched types 'const _CharT*' and 'void'
- cannot convert 'account.BankAccount::displayAcctBalance()' (type 'void') to type 'const
unsigned char*'
- mismatched types 'const std::basic_string<_CharT, _Traits, _Alloc>' and 'void'
- candidates are:
- no match for'operator<<' (operand types are 'std::basic_ostream<char>' and 'void')
- cannot convert 'account.BankAccount::displayAcctBalance()' (type 'void') to type 'unsigned char'
- cannot convert 'account.BankAccount::displayAcctBalance()' (type 'void') to type 'signed char'
- cannot convert 'account.BankAccount::displayAcctBalance()' (type 'void') to type 'char'
- deduced conflicting types for parameter '_CharT' ('char' and 'void')
I'm at my wit's end with this problem. I cannot figure it out. Can someone help? Your insights will be appreciated! My code can be seen below.
tcollar
***************
BankAccount.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#ifndef BACNKACCOUNT_H_
#define BACNKACCOUNT_H_
usingnamespace std;
class BankAccount
{
public:
BankAccount (int);
void setAcctBalance (int);
int getAcctBalance ();
int Credit (int);
int Debit (int);
void displayAcctBalance ();
private:
int AcctBalance;
};
#endif /* BACNKACCOUNT_H_ */
#include <iostream>
#include "BankAccount.h"
usingnamespace std;
int main()
{
int number, amount;
BankAccount account ( 238 ); //Instantiating the account. Initial balance=$238.
cout<<"What do you want to do?\n" <<endl;
cout<<"To credit to you account, please press 1;\n" <<"To debit from you account, please press 2;\n"<<endl;
cin>>number;
while (number!=1 && number !=2)
{
cout<<"Wrong number. Please try again:\n" <<endl;
cout<<"To credit to you account, please press 1;\n" <<"To debit from you account, please press 2;\n"<<endl;
cin>>number;
if (number==1)
{
cout<<"Please enter amount:\n" <<endl;
cin>>amount;
cout<< "Your original balance is "<<account.displayAcctBalance()<<"!\n"<<endl;
account.Credit(amount);
cout<< "Your current balance is "<<account.displayAcctBalance()<<"!\n"<<endl;
break;
}
elseif (number==2)
{
cout<<"Please enter amount:\n" <<endl;
cin>>amount;
cout<< "Your original balance is "<<account.displayAcctBalance()<<"!\n"<<endl;
account.Debit(amount);
cout<< "Your current balance is "<<account.displayAcctBalance()<<"!\n"<<endl;
break;
}
}
}
dont put using namespace in every file, ...and is a very bad habit.
This sounds like a serious problem. I did this only because Deitel encouraged readers to include "using namespace std" in the files so one doesn't have to type the pesky little "std::" every time! Duly noted.