By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

Overloading += operator

100+
P: 143
OK I have this assignment where I am to overload the << and >> operators. Got it done. Next n the list is to overload the += and -= operators to allow an increase in a balance of a savings account of $1000.

I'm stuck on how to do this. The book is not very clear on this and I'm not getting it yet.

So far this is what I've got:
Expand|Select|Wrap|Line Numbers
  1. in SavingsAccount.h (header file)
  2.  
  3. const SavingsAccount &SavingsAccount::operator+=(double money);
  4.  
  5.  
  6. in SavingsAccount.cpp (class file)
  7.  
  8. const SavingsAccount &SavingsAccount::operator+=(double money)
  9. {
  10.       double addBalance = savingsBalance + money;
  11.       return addBalance:
  12. }
  13.  
  14.  
  15. in main:
  16. {
  17.       .....
  18.       count<<saver4.getBalance() += 1000<<endl;
  19. }
  20.  
my errors are:
1. mismatch on formal parameter list
2. '<<' :unable to resolve function overload
3. 'savingsBalance' : undeclared identifier
4. 'return': cannot convert from 'double' to 'const SavingsAccount &'

I've used savingsBalance elsewhere in this class and have not gotten error #3 until now.
any ideas?
Mar 9 '07 #1
Share this Question
Share on Google+
8 Replies


P: 93
OK I have this assignment where I am to overload the << and >> operators. Got it done. Next n the list is to overload the += and -= operators to allow an increase in a balance of a savings account of $1000.

I'm stuck on how to do this. The book is not very clear on this and I'm not getting it yet.

So far this is what I've got:

in SavingsAccount.h (header file)

const SavingsAccount &SavingsAccount::operator+=(double money);


in SavingsAccount.cpp (class file)

const SavingsAccount &SavingsAccount::operator+=(double money)
{
double addBalance = savingsBalance + money;
return addBalance:
}


in main:
{
.....
count<<saver4.getBalance() += 1000<<endl;
}

my errors are:
1. mismatch on formal parameter list
2. '<<' :unable to resolve function overload
3. 'savingsBalance' : undeclared identifier
4. 'return': cannot convert from 'double' to 'const SavingsAccount &'

I've used savingsBalance elsewhere in this class and have not gotten error #3 until now.
any ideas?


did you type "count<<" or "cout<<" because it should be "cout<<".
Check for whether there should be a variable "double savingsBalance" in the SavingsAccount class.
What is the part of the code that u wrote, and what part did the book contain?
Plz post the full code given in the book.
Mar 9 '07 #2

Expert 100+
P: 1,510
assuming you have a public data member which has been initialised
Expand|Select|Wrap|Line Numbers
  1.  double savingsBalance;   
  2.  
operator += should be something like
Expand|Select|Wrap|Line Numbers
  1. const SavingsAccount &SavingsAccount::operator+=(double money)
  2. {
  3. savingsBalance = savingsBalance + money;
  4. return *this;
  5. }
  6.  
Mar 9 '07 #3

100+
P: 143
All of the code listed above is my code. None of it came from the book.

It was my typo to put 'count'. In te source code I did use 'cout'

I changed the return statement to say "return *this;" 1 error message is gone. Only 3 more to go. Error #4 disappeared.
Mar 9 '07 #4

100+
P: 143
Here's what I have so far:

[HTML]const SavingsAccount &SavingsAccount::operator+=(const SavingsAccount &money)
{
double addBalance = savingsBalance + money;
return *this;
}[/HTML]

[HTML]ostream &operator<<(ostream &output, const SavingsAccount &saver)
{
SavingsAccount::header();
output<<saver.getNumber()<<setw(10)<<saver.firstNa me<<" "<<saver.lastName
<<setw(15)<<saver.savingBalance<<setw(8)<<SavingsA ccount::getInterestRate()*100
<<'%';

output<<endl<<endl;
return output;
}

istream &operator>>(istream &input, SavingsAccount &saver)
{

input>>saver.firstName;
input>>saver.lastName;
input>>saver.savingBalance;
return input;
}[/HTML]

[HTML]void main()
{
SavingsAccount saver4;
cout<<"Enter a first name, press \'ENTER\' enter a last name,\n press \'ENTER\' enter a balance: "<<endl;
cin>>saver4;
cout<<saver4<<endl;
cout<<saver4.getBalance()+=1000<<endl;
}[/HTML]

These functions use other member functions to assign an account number, name, balance, and static interest rate. I'm trying to now overload += to add 1000 to the balance.

Help! I'm stuck.
Mar 9 '07 #5

Expert 100+
P: 1,510
for a start I think operator+=() should look something like
Expand|Select|Wrap|Line Numbers
  1. SavingsAccount &SavingsAccount::operator+=(double money)
  2. {
  3. savingsBalance = savingsBalance + money;
  4. return *this;
  5. }
what other problems do you have
Mar 9 '07 #6

100+
P: 143
OK, That clears up a couple of problems.

I have:
[HTML]SavingsAccount &SavingsAccount::operator+=(double money)
{
double addBalance = savingBalance + money;
return *this;
}[/HTML]

I'm down to 2 errors:

1. Mismatch in formal parameter list //I don't know what's going on here. I thought the header file matched

2. '<<' : unable to resolve function overload //I think this has something to do with the operator overload friend functions posted earlier, but I don't know what to look for.
Mar 9 '07 #7

Ganon11
Expert 2.5K+
P: 3,652
I agree with horace, but if you'd like to have the option of adding two SavingsAccount object (rather than adding a double to a SavingsAccount object), then you'll need the header with a const SavinsAccount object in it. However, you cannot add a SavingsAccount to your double - you will have to refer to the proper variable in the other SavinsAccount object, add it to the variable in the current object, and then return *this.
Mar 9 '07 #8

100+
P: 143
I'm still confused on this. I'm still trying to work out what questions to ask.

I'll give it a try a little later when the kids take a nap.

Thanks for the input. I'll let you know.
Mar 10 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.