C++中出现很长"No match for 'operator+'"错误

Getting very long "No match for 'operator+'" error in C++

本文关键字:for 错误 operator+ match No C++      更新时间:2023-10-16

我已经不再使用C++了,正在为我正在学习的课程编写一个"Bank"类。每次尝试编译Bank.cpp文件时,我都会遇到一个长错误。我怀疑我遗漏了一些显而易见的东西,但我不知道是什么,因为这个错误对我来说没有任何意义

以下是我正在编写的无法编译的cpp文件:

#include <iostream>
#include "Bank.h"
Bank::Bank(): savings(0), checking(0) { }
Bank::Bank(double savings_amount, double checking_amount): savings(savings_amount), checking(checking_amount) { }
void Bank::deposit(double amount, string account)
{
if (account == "S") {
savings = savings + amount;
} else {
checking += amount;
}
}
void Bank::withdraw(double amount, string account)
{
if (account == "S") {
savings -= amount;
} else {
checking -= amount;
}
}
void Bank::transfer(double amount, string account)
{
if (account == "S") {
savings -= amount;
checking += amount;
} else {
checking -= amount;
}
}
void Bank::transfer(double amount, string account)
{
if (account == "S") {
savings -= amount;
checking += amount;
} else {
checking -= amount;
savings += amount;
}
}
void Bank::print_balances()
{
cout << "Savings: $ " << savings;
cout << "Checking: $ " << checking;
}

这是头文件:

#ifndef BANK_H
#define BANK_H
#include <string>
#include "Account.h"
using namespace std;
class Bank {
private:
Account savings;
Account checking;
public:
Bank();
Bank(double savings_amount, double checking_amount);
void deposit(double amount, string account);
void withdraw(double amount, string account);
void transfer(double amount, string account);
void print_balances();
};
#endif

这是顶部引用的"Account.h"头文件:

#ifndef ACCOUNT_H
#define ACCOUNT_H

class Account {
private:
double balance;
double interest_rate;
public:
Account();
Account(double amount, double rate);
void deposit(double);
bool withdraw(double);
double query();
void set_interest_rate(double rate);
double get_interest_rate();
void add_interest();
};
#endif

这是我收到的一个神秘的错误:

Bank.cpp:在成员函数'void Bank::deposit(double,std::string)'中:Bank.cpp:17:错误:"(银行*)this)->银行::储蓄+金额"中的"operator+"不匹配Bank.cpp:19:错误:"(Bank*)this)->Bank::checking+=amount"中的"operator+="不匹配Bank.cpp:在成员函数"void Bank::draw(double,std::string)"中:Bank.cpp:26:错误:不匹配'operator-='in'((Bank*)this)->Bank::savement-=amount'Bank.cpp:28:错误:"operator-="in"((Bank*)this)->Bank::checking-=amount"不匹配Bank.cpp:在成员函数"void Bank::transfer(double,std::string)"中:Bank.cpp:35:错误:"operator-="in"((Bank*)this)->Bank::saving-=amount"不匹配Bank.cpp:36:错误:"(Bank*)this)->Bank::checking+=amount"中的"operator+="不匹配Bank.cpp:38:错误:"operator-="in"((Bank*)this)->Bank::checking-=amount"不匹配Bank.cpp:39:错误:在'((Bank*)this)->Bank::储蓄+=amount'中没有匹配的'operator+='Bank.cpp:在成员函数"void Bank::print_balands()"中:Bank.cpp:45:错误:"operator<"不匹配<'在'std::operator<lt;[with_Traits=std::char_Traits](((std::basic_stream>&)(&std::cout)),((const-char*)"Savings:$")<lt;((银行*)本)->银行::储蓄'//usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../include/c++/4.4.7/ostream:108:注意:候选为:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(std::basic_stream<_CharT,_Traits>&()/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:117:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(std::basic_ios<_CharT,_Traits>&()/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:127:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(std::ios_base&()(std:;ios_base\amp;))[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:165:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(长整型)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:169:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(长无符号int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:173:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(bool)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/bits/sostream.tcc:91:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(短int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:180:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(短无符号int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/bits/sostream.tcc:105:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:191:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(unsigned int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:200:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(long-long-int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:204:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(long-long unsigned int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:209:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(双)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:213:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(浮点)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:221:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(长双)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:225:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(const void)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/bits/sostream.tcc:119:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(std::basic_streambuf<_CharT,_Traits>)[其中_CharT=char,_Trait=std::char_Traits]Bank.cpp:46:错误:"operator<"不匹配<'在'std::operator<lt;[with_Traits=std::char_Traits](((std::basic_stream>&)(&std::cout)),((const-char)"Checking:$")<lt;((银行*)本)->银行::支票'//usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../include/c++/4.4.7/ostream:108:注意:候选为:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(std::basic_stream<_CharT,_Traits>&()/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:117:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(std::basic_ios<_CharT,_Traits>&()/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:127:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(std::ios_base&()(std:;ios_base\amp;))[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:165:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(长整型)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:169:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(长无符号int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:173:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(bool)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/bits/sostream.tcc:91:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(短int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:180:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(短无符号int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/bits/sostream.tcc:105:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:191:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(unsigned int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:200:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(long-long-int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:204:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(long-long unsigned int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:209:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(双)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:213:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(浮点)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:221:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(长双)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:225:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(const void)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/bits/sostream.tcc:119:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(std::basic_streambuf<_CharT,_Traits>)[其中_CharT=char,_Trait=std::char_Traits][004709613@jb359-5Lab1]$Bank.cpp:在成员函数"void Bank::deposit(double,std::string)"中:-bash:意外令牌(' [004709613@jb359-5 Lab1]$ Bank.cpp:17: error: no match for ‘operator+’ in ‘((Bank*)this)->Bank::savings + amount’ -bash: syntax error near unexpected token('[004709613@jb359-5Lab1]$Bank.cpp:19:错误:"(Bank)this)->Bank::checking+=amount"中的"operator+="不匹配-bash:意外令牌(' [004709613@jb359-5 Lab1]$ Bank.cpp: In member function ‘void Bank::withdraw(double, std::string)’: -bash: syntax error near unexpected token('[004709613@jb359-5Lab1]$Bank.cpp:26:错误:"operator-="in"((Bank*)this)->Bank::saving-=amount"不匹配-bash:意外令牌(' [004709613@jb359-5 Lab1]$ Bank.cpp:28: error: no match for ‘operator-=’ in ‘((Bank*)this)->Bank::checking -= amount’ -bash: syntax error near unexpected token('[004709613@jb359-5Lab1]$Bank.cpp:在成员函数"void Bank::transfer(double,std::string)"中:-bash:意外令牌(' [004709613@jb359-5 Lab1]$ Bank.cpp:35: error: no match for ‘operator-=’ in ‘((Bank*)this)->Bank::savings -= amount’ -bash: syntax error near unexpected token('[004709613@jb359-5Lab1]$Bank.cpp:36:错误:"(Bank*)this)->Bank::checking+=amount"中的"operator+="不匹配-bash:意外令牌(' [004709613@jb359-5 Lab1]$ Bank.cpp:38: error: no match for ‘operator-=’ in ‘((Bank*)this)->Bank::checking -= amount’ -bash: syntax error near unexpected token('[004709613@jb359-5Lab1]$Bank.cpp:39:错误:"(Bank*)this)->Bank::saves+=amount"中的"operator+="不匹配-bash:意外令牌(' [004709613@jb359-5 Lab1]$ Bank.cpp: In member function ‘void Bank::print_balances()’: -bash: syntax error near unexpected token('[004709613@jb359-5Lab1]$Bank.cpp:45:错误:"operator<lt;'在'std::operator<lt;[with_Traits=std::char_Traits](((std::basic_stream>&)(&std::cout)),((const-char*)"Savings:$")<lt;((银行*)本)->银行::储蓄'//usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../include/c++/4.4.7/ostream:108:注意:候选为:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(std::basic_stream<_CharT,_Traits>&()/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:117:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(std::basic_ios<_CharT,_Traits>&()/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:127:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(std::ios_base&()(std:;ios_base\amp;))[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:165:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(长整型)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:169:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(长无符号int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:173:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(bool)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/bits/sostream.tcc:91:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(短int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:180:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(短无符号int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/bits/sostream.tcc:105:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:191:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(unsigned int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:200:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(long-long-int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:204:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(long-long unsigned int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:209:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(双)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:213:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(浮点)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:221:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(长双)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:225:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(const void)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/bits/sostream.tcc:119:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(std::basic_streambuf<_CharT,_Traits>)[其中_CharT=char,_Trait=std::char_Traits]Bank.cpp:46:错误:"operator<"不匹配<'在'std::operator<lt;[with_Traits=std::char_Traits](((std::basic_stream>&)(&std::cout)),((const-char)"Checking:$")<lt;((银行*)本)->银行::支票'//usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../include/c++/4.4.7/ostream:108:注意:候选为:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(std::basic_stream<_CharT,_Traits>&()/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:117:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(std::basic_ios<_CharT,_Traits>&()/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:127:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(std::ios_base&()(std:;ios_base\amp;))[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:165:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(长整型)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:169:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(长无符号int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:173:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(bool)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/bits/sostream.tcc:91:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(短int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:180:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(短无符号int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/bits/sostream.tcc:105:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:191:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(unsigned int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:200:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(long-long-int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:204:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(long-long unsigned int)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:209:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(双)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:213:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(浮点)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:221:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(长双)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/ostream:225:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(const void)[其中_CharT=char,_Traits=std::char_Traits]/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../include/c++/4.4.7/bits/sostream.tcc:119:注意:std::basic_stream&lt_CharT,_Traits>&std::basic_stream&lt_CharT,_Traits>::运算符<lt;(std::basic_streambuf<_CharT,_Traits>*)[其中_CharT=char,_Trait=std::char_Traits]

有人知道我做错了什么吗?我确信这是非常愚蠢的事情。我也可以发布头文件,但由于远程编辑问题,很难做到这一点。

您需要为Account类定义+运算符,以便它知道该做什么。由于您试图添加一个带有double的Account对象,因此您需要这个。

double Account::operator+(const double value) const {
return balance + value;
}

如果你想添加两个帐户对象,你可以用这个重载。

Account Account::operator+(const Account &other) const {
return Account(balance + other.balance, interest_rate);
}

如果你不想在类中添加额外的运算符,那么不要写:

savings = savings + amount;

使用Account类已经声明的方法,并写入:

savings.deposit(amount);

我怀疑您将保存和检查私人成员声明为"Account",而不是双重声明。即使这是您想要的设计,也不能在没有显式定义运算符+(const Account&)的情况下添加用户定义的类对象。

在源代码的这一行中:

savings = savings + amount;

savings具有Account的类型,但是amountdouble。除非在Account类(如)上定义+成员运算符

Account operator+(const double value) const;

或者像这样的全局+运算符(可能作为friend函数)

Account operator+(const Account& account, const double value);

那么你就不能做这样的加法了。