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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.