为什么我的重载运算符返回零
Why is my overloading operator returning zero?
无论出于什么原因,当我试图从重载函数返回值时,它都返回零。前几天我刚开始使用重载,所以我对它很陌生,所以如果这是一个简单的错误,我很抱歉。
这很奇怪,因为当我cout
函数中的值时,它正好显示了我想要看到的内容,然而当我返回它时,它仍然返回一个空值,即零。
功能如下:
double operator+=(double ls, Account& rs){
//cout << ls << endl;
//cout << rs._balance+ ls << endl;
return ls+=rs._balance;
//return rs._balance+=ls;
}
这是完整的.cpp文件,包括以下函数:
#define _CRT_SECURE_NO_WARNINGS
#include <cstring>
#include <iomanip>
#include "Account.h"
using namespace std;
namespace sict{
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()const{
for(int x = 0; x < 40; x++){
if(_name[x] == ' ')
x = 40;
else
cout << _name[x];
}
cout << ": $" << setprecision(2) << fixed << _balance;
}
Account Account::operator+(Account ls) {
return ls._balance + _balance;
}
double operator+=(double ls, Account& rs){
//cout << ls << endl;
//cout << rs._balance+ ls << endl;
return ls+=rs._balance;
//return rs._balance+=ls;
}
Account Account::operator+=(Account& ls){
return _balance+=ls._balance;
}
Account::Account(const char name[]){
strncpy(_name, name, 40);
}
char* Account::getName(){
return _name;
}
double Account::getBal(){
return _balance;
}
std::ostream& operator<<(std::ostream& ls, Account& rs){
rs.display();
return ls;
}
Account& Account::operator=(Account& ls){
if( !strcmp(ls._name,"") &&ls._balance > 0)
{
strcpy(_name, "Saving");
}
_balance = ls._balance;
//strcpy(_name, ls._name);
return *this;
}
char* Account::operator=(char* ls){
strcpy(_name, ls);
return _name;
}
}
这是头文件:
#ifndef SICT_ACCOUNT_H__
#define SICT_ACCOUNT_H__
#include <iostream>
#include <cstring>
#include <iomanip>
namespace sict{
class Account{
char _name[41];
double _balance;
public:
Account();
Account::Account(double balance);
Account::Account(const char name[], double balance);
Account::Account(const char name[]);
Account& operator=(Account& ls);
Account operator+=(Account& ls);
char* operator=(char* ls);
void display()const;
double getBal();
char* getName();
friend double operator+=(double ls, Account& rs);
Account operator+(Account ls);
};
std::ostream& operator<<(std::ostream& ls, Account& rs);
};
#endif
主要内容如下:
#include <iostream>
#include <string>
#include "Account.h"
using namespace sict;
using namespace std;
int main()
{
Account A;
Account B("Saving", 10000.99);
Account C("Checking", 100.99);
double value = 0;
cout << A << endl << B << endl << C << endl << "--------" << endl;
A = B + C;
A = "Joint";
cout << A << endl << B << endl << C << endl << "--------" << endl;
A = B += C;
cout << A << endl << B << endl << C << endl << "--------" << endl;
value += A;
value += B;
value += C;
cout << "Total balance: " << value << endl;
return 0;
}
对于
value += A;
value += B;
value += C;
为了工作,您必须将重载运算符函数的参数更改为引用。现在,它通过值传递。该值在函数中被修改,但这不会改变调用函数中的值。
代替
double operator+=(double ls, Account& rs){
使用
double operator+=(double& ls, Account& rs){
// ^^
为了实现这一点,您需要通过引用捕获左手参数。
double operator+=(double ls, Account& rs)
需要
double operator+=(double& ls, Account& rs)
相关文章:
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 使用赋值运算符重载从类中返回jobject
- 为什么在我的函数类型后使用引用运算符 (&) 允许我修改它返回的值?
- 为什么将值返回函数传递给重载=运算符对运算符函数有效,而对其他运算符无效
- 重载更少,则运算符返回相反的布尔值
- 是否所有C++运算符都返回一些内容
- 使用重载 [] 运算符返回 std::map() 的可赋值
- 三元运算符在返回语句中给出意外的结果
- 为什么重载运算符可以返回其返回类型以外的其他内容?
- 当返回语句时,逗号运算符、大括号初始化列表和 std::unique_ptr 组合在一起
- 返回 bool 作为 CPP 中 bool 运算符 [] 中的值
- 运算符重载C++类中的引用返回
- 如何使用运算符+重载函数正确返回对象?
- 我的运算符重载没有在我的 int main 中返回?
- 在C++中,运算符 sizeof 返回什么数据类型?
- 为什么我的超载"+"运算符返回的总额错误?
- 为什么在通过引用返回运算符分配时取消引用'this'指针?
- 如何在C++中正确地返回运算符(*,/,-,+)
- C++ 函数返回 &&& 运算符
- 返回运算符类型=-引用或值