gpt4 book ai didi

c++ - 重载运算符问题 C++

转载 作者:行者123 更新时间:2023-11-30 05:26:02 25 4
gpt4 key购买 nike

我遇到了一个似乎无法正常工作的重载运算符的问题。以下是我的代码。

帐户.H

#ifndef SICT_ACCOUNT_H__
#define SICT_ACCOUNT_H__

#include <iostream>

#define MAX_NAME 40

namespace sict
{
class Account
{
char name_[MAX_NAME+1];
double balance_;
public:
Account();
Account(double);
Account(const char*, double = 0.0);
void display(bool = true) const;
Account& operator=(const Account&);
Account& operator+=(const Account&);
friend Account operator+(Account&, const Account&);
};
std::ostream& operator<<(std::ostream& os, const Account& a);
};

#endif

帐户.CPP

 #include <cstring>
#include <iomanip>
#include "Account.h"

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)
{
name_[MAX_NAME] = '\0';
strncpy(name_, name, MAX_NAME);
balance_ = balance;
}

void Account::display(bool gotoNewline) const
{
std::cout << (name_[0] ? name_: "No Name") << ": $" << std::setprecision(2) << std::fixed << balance_;
if(gotoNewline)
{
std::cout << std::endl;
}
}

std::ostream& operator<<(std::ostream& os, const Account& a)
{
a.display();
return os;
}

Account& Account::operator=(const Account& c)
{
balance_ = c.balance_;
name_[MAX_NAME] = '\0';
strncpy(name_, c.name_, MAX_NAME);

return *this;
}

Account& Account::operator+=(const Account& b)
{
balance_ += b.balance_;
return *this;
}

Account operator+(Account& a, const Account& b)
{
return a.balance_ + b.balance_;
}


}

主要

#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;
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;
}

我应该得到的输出是

A: No Name: $0.00
B: Saving: $10000.99
C: Checking: $100.99
--------
A: No Name: $10101.98
B: Saving: $10000.99
C: Checking: $100.99
--------
A: Joint: $10101.98
B: Saving: $10000.99
C: Checking: $100.99
--------
A: Saving: $10101.98
B: Saving: $10101.98
C: Checking: $100.99
--------
A: Saving: $10302.98
B: Saving: $10302.98
C: Checking: $201.00

这是我现在得到的结果

A: No Name: $0.00
B: Saving: $10000.99
C: Checking: $100.99
--------
A: No Name: $10101.98
B: Saving: $10000.99
C: Checking: $100.99
--------
A: Joint: $0.00
B: Saving: $10000.99
C: Checking: $100.99
--------
A: Saving: $10101.98
B: Saving: $10101.98
C: Checking: $100.99
--------
A: Saving: $10302.98
B: Saving: $10302.98
C: Checking: $201.00
--------

如您所见,显示的整数值为 0:

A: union :$0.00。

我似乎无法弄清楚这一点。任何帮助表示赞赏!

最佳答案

displayABC(a, b, c);
a = b + c;
displayABC(a, b, c);
a = "Joint"; //The Problem!!
displayABC(a, b, c);
a = b += c;
displayABC(a, b, c);
a = b += c += 100.01;
displayABC(a, b, c);

您还没有为字符串对象定义特定的 operator=,因此,编译器在 Constructor Account(const char name[], double balance = 0.0 ),因为没有指定余额,所以将余额设置为 0。

您应该将关键字 explicit 添加到构造函数以防止它这样做,或者添加一个额外的重载以允许在不改变平衡的情况下设置名称。

explicit Account::Account(const char name[], double balance)
{
name_[MAX_NAME] = '\0';
strncpy(name_, name, MAX_NAME);
balance_ = balance;
}

顺便说一下,在 C++ 世界中,您应该将 const char * 类型的所有引用替换为 std::string,并将其存储在您的对象中.唯一应该使用 c 字符串的情况是必须与 c 库交互时。

关于c++ - 重载运算符问题 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37999332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com