1

I'm attempting to overload a += operator with the following line:

a = b += c += 100.01; 

but my second += in that code shows an error as it matches no implementation i guess?

Heres my full relevant code:

main.cpp:

 #include <iostream> #include "Account.h" using namespace sict; void displayABC(const Account& a, const Account& b, const Account& c) { std::cout << "A: " << a << std::endl << "B: " << b << std::endl << "C: " << c << std::endl << "--------" << std::endl; } int main() { Account a("No Name"); Account b("Saving", 10000.99); Account c("Checking", 100.99); displayABC(a, b, c); a = b + c; displayABC(a, b, c); a = "Joint"; displayABC(a, b, c); a = b += c; displayABC(a, b, c); a = b += c += 100.01; displayABC(a, b, c); return 0; } 

Account.h (Relevant definitions)

class Account{ public: friend Account operator+(const Account &p1, const Account &p2); Account& operator+=(Account& s1); Account & operator=(const char name[]); friend double & operator+=(double & Q, Account & A); Account & operator=(Account D); }; Account operator+(const Account &p1, const Account &p2); double operator+=(double& d, const Account& a); }; 

Account.cpp (relevant implementations)

Account& Account::operator+=(Account &s1) { double b = this->balance_ + s1.balance_; this->balance_ = b; return *this; } Account & Account::operator=(Account D) { strcpy(name_, D.name_ ); this->balance_ = D.balance_; return *this; } Account & Account::operator=(const char name[]) { strcpy_s(name_, name); return *this; } double & operator+=(double & Q, Account & A) { Q = Q + A.balance_; return Q; } 

So my question is, how do i change my implementations correctly to run this function: a = b += c += 100.01;

Thank you.

2
  • 4
    You should also show what the error message is instead of simply telling us that it "shows an error" Commented Mar 10, 2016 at 15:14
  • 1
    This might be a bit off topic, but why the hell do you want to do this. Whats wrong with using functions, in combination with some range/vector type. Just because operator overloading is a thing, it doesn't mean you should. Commented Mar 10, 2016 at 15:16

1 Answer 1

1

In this statement

a = b += c += 100.01; 

at first expression

c += 100.01 

is evaluated. However the class does not have a corresponding overloaded operator.

On the other hand if the class has a conversion constructor that converts an object of type double to type Account nevertheless the corresponding operatpr += should be declared like

Account& operator+=(const Account& s1); ^^^^^ 

Also take into account that these declarations

friend double & operator+=(double & Q, Account & A); 

and

double operator+=(double& d, const Account& a); 

differ.

Sign up to request clarification or add additional context in comments.

1 Comment

Well, it almost does: Account& operator+=(Account& s1); and friend double & operator+=(double & Q, Account & A);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.