重载运算符无法正常工作
Overloaded Operator not working properly?
我在编译程序时遇到错误,不确定如何使用适当的函数来解决此问题。我将在下面发布主文件、标题文件和 CPP 文件,并给出所需的结果。任何帮助/提示不胜感激,谢谢!
我得到的错误:
Error C2679 binary '+=': no operator found which takes a right-hand operand of type 'double'
更新:我已经修复了代码以更新"="运算符重载,除了 1 行输出外,它仍在工作。
这是我得到的输出:
答:无名称:$0.00
B: 节省: $10000.99
C:支票:$100.99
A: 无名称: $10101.98
B: 节省: $10000.99
C:支票:$100.99
答:关节:$0.00
B: 节省: $10000.99
C:支票:$100.99
A: 节省: $10101.98
B: 节省: $10101.98
C:支票:$100.99
A: 节省: $10302.98
B: 节省: $10302.98
C: 支票: $201.00
由于某种原因,"联合"余额显示为 0,我不确定为什么。 它应该显示为 $10101.98
主要:
#include <iostream>
#include "Account.h"
using namespace std;
void displayABC(const Account& A,
const Account& B,
const Account& C) {
cout << "A: " << A << endl << "B: " << B << endl
<< "C: " << C << endl << "--------" << endl;
}
int main() {
Account A;
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;
}
源文件:
#include <iomanip>
#include <cstring>
#include "Account.h"
using namespace std;
Account::Account() {
name_[0] = 0;
balance_ = 0;
}
Account::Account(double balance) {
name_[0] = 0;
balance_ = balance;
}
Account::Account(const char name[], double balance) {
strncpy(name_, name, 40);
name_[40] = 0;
balance_ = balance;
}
void Account::display(bool gotoNewline)const {
cout << (name_[0] ? name_ : "No Name") << ": $" << setprecision(2) << fixed << balance_;
if (gotoNewline) cout << endl;
}
Account& Account::operator+=(Account& other) {
balance_ += other.balance_;
return *this;
}
Account& Account::operator=(const Account& ls) {
strcpy(name_, ls.name_);
balance_ = ls.balance_;
return *this;
}
Account operator+(const Account &one, const Account &two) {
return Account(one.balance_ + two.balance_);
}
ostream& operator<<(ostream& os, const Account& A) {
A.display();
return os;
}
头文件:
#ifndef _ACCOUNT_H__
#define _ACCOUNT_H__
#include <iostream>
class Account {
char name_[41];
double balance_;
public:
Account();
Account(double balance);
Account(const char name[], double balance = 0.0);
void display(bool gotoNewline = true)const;
Account& operator+=(Account& other);
Account& operator=(const Account & other);
friend Account operator+(const Account &one, const Account &two);
};
std::ostream& operator<<(std::ostream& os, const Account& A);
};
#endif
将+=
运算符方法更改为:
Account& operator+=(const Account& other);
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- sdl软件渲染器不工作,工作在硬件加速的一个
- C++程序已停止工作-求解常微分方程