我尝试使用以下行重载+ =运算符:
<div itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
<!-- … -->
</div>
<div itemprop="image" itemscope itemtype="http://schema.org/ImageObject">
<!-- … -->
</div>
但我的第二个+ =在该代码中显示错误,因为它匹配没有实现我猜?
以下是我的完整相关代码:
main.cpp中:
a = b += c += 100.01;
Account.h(相关定义)
#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.cpp(相关实施)
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& 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;
}
谢谢。
答案 0 :(得分:1)
在本声明中
a = b += c += 100.01;
首先表达
c += 100.01
评估。但是,该类没有相应的重载运算符。
另一方面,如果类具有将double类型的对象转换为类型Account的转换构造函数,则相应的operatpr +=
应声明为
Account& operator+=(const Account& s1);
^^^^^
还要考虑到这些声明
friend double & operator+=(double & Q, Account & A);
和
double operator+=(double& d, const Account& a);
不同。