我没有使用C++练习,正在为我正在学习的课程写一个"银行"课程.每次尝试编译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;
}
Run Code Online (Sandbox Code Playgroud)
这是头文件:
#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
Run Code Online (Sandbox Code Playgroud)
这里是顶部引用的"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
Run Code Online (Sandbox Code Playgroud)
这是我收到的神秘错误:
Bank.cpp: In member function 'void Bank::deposit(double, std::string)’: Bank.cpp:17: error: no match for 'operator+’ in '((Bank*)this)->Bank::savings + amount’ Bank.cpp:19: error: no match for 'operator+=’ in '((Bank*)this)->Bank::checking += amount’ Bank.cpp: In member function 'void Bank::withdraw(double, std::string)’: Bank.cpp:26: error: no match for 'operator-=’ in '((Bank*)this)->Bank::savings -= amount’ Bank.cpp:28: error: no match for 'operator-=’ in '((Bank*)this)->Bank::checking -= amount’ Bank.cpp: In member function 'void Bank::transfer(double, std::string)’: Bank.cpp:35: error: no match for 'operator-=’ in '((Bank*)this)->Bank::savings -= amount’ Bank.cpp:36: error: no match for 'operator+=’ in '((Bank*)this)->Bank::checking += amount’ Bank.cpp:38: error: no match for 'operator-=’ in '((Bank*)this)->Bank::checking -= amount’ Bank.cpp:39: error: no match for 'operator+=’ in '((Bank*)this)->Bank::savings += amount’ Bank.cpp: In member function 'void Bank::print_balances()’: Bank.cpp:45: error: no match for 'operator<<’ in 'std::operator<< [with _Traits = std::char_traits](((std::basic_ostream >&)(& std::cout)), ((const char*)"Savings: $ ")) << ((Bank*)this)->Bank::savings’ /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& ()(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:117: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& ()(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:127: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& ()(std :: ios_base&))[with _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_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(long int)[with _CharT = char,_Traits = std :: char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:169:note:std: :basic_ostream <_CharT,_ Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(long unsigned int)[with _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_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT, _Traits> ::运算符<<(bool)[with _CharT = char,_Traits = std :: char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4 .7/bits/ostream.tcc:91:注意:std :: basic_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(short int)[with _CharT = char,_Traits = std :: char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:180:note:std :: basic_ostream <_CharT,_ Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(short unsigned int)[with _CharT = char,_Traits = std :: char_traits]/usr/lib/gcc/x86_64-redhat- linux/4.4.7 /../../../../ include/c ++/4.4.7/bits/ostream.tcc:105:注意:std :: basic_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(int)[with _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_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(unsigned int)[with _CharT = char,_Traits = std :: char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:200:note:std :: basic_ostream <_CharT, _Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(long long int)[with _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_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(long long unsigned int)[with _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_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(double)[with _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_ostream < _CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(float)[with _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_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(long double)[与_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_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(const void)[with _CharT = char,_ Traits = std :: char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7 /bits/ostream.tcc:119:注意:std :: basic_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(std :: basic_streambuf <_CharT,_Traits> )[with _CharT = char,_Traits = std :: char_traits] Bank.cpp:46:错误:'std :: operator <<中没有匹配'operator <<'[与_Traits = std :: char_traits]((std :: basic_ostream> &)(&std :: cout)),((const char)"Checking:$"))<<((Bank*)this) - > Bank :: checking'/ usr/lib/gcc/x86_64-redhat- linux/4.4.7 /../../../../ include/c ++/4.4.7/ostream:108:注意:候选人是:std :: basic_ostream <_CharT,_Traits>&std :: basic_ostream < _CharT,_Traits> :: operator <<(std :: basic_ostream <_CharT,_Traits>&()(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:117: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& ()(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:127: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& ()(std :: ios_base&))[with _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_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(long int)[with _CharT = char,_Traits = std :: char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:169:note:std: :basic_ostream <_CharT,_ Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(long unsigned int)[with _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_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT, _Traits> ::运算符<<(bool)[with _CharT = char,_Traits = std :: char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4 .7/bits/ostream.tcc:91:注意:std :: basic_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(short int)[with _CharT = char,_Traits = std :: char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:180:note:std :: basic_ostream <_CharT,_ Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(short unsigned int)[with _CharT = char,_Traits = std :: char_traits]/usr/lib/gcc/x86_64-redhat- linux/4.4.7 /../../../../ include/c ++/4.4.7/bits/ostream.tcc:105:注意:std :: basic_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(int)[with _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_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(unsigned int)[with _CharT = char,_Traits = std :: char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:200:note:std :: basic_ostream <_CharT, _Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(long long int)[with _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_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(long long unsigned int)[with _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_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(double)[with _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_ostream < _CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(float)[with _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_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(long double)[与_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_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(const void)[with _CharT = char,_ Traits = std :: char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7 /bits/ostream.tcc:119:注意:std :: basic_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(std :: basic_streambuf <_CharT,_Traits> )[with _CharT = char,_Traits = std :: char_traits] [004709613 @ jb359-5 Lab1] $ 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-5 Lab1] $ Bank.cpp:19:错误:'(+(')这里的'operator + ='不匹配 - > Bank :: checking + = amount'-bash:意外附近的语法错误代币(' [004709613@jb359-5 Lab1]$ Bank.cpp: In member function ‘void Bank::withdraw(double, std::string)’: -bash: syntax error near unexpected token('[004709613 @ jb359-5 Lab1] $ Bank.cpp:26:错误:'((Bank*)this'中'operator- ='不匹配 - > Bank :: savings - = amount'-bash:syntax意外令牌附近的错误(' [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-5 Lab1] $ 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-5 Lab1] $ Bank.cpp:36:错误:'(+*)中'运算符+ ='不匹配 - >银行::检查+ =金额'-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-5 Lab1] $ Bank.cpp:39:错误:'运营商+ ='中没有匹配'((银行*)这个) - >银行::储蓄+ =金额'-bash:语法错误附近意外的标记(' [004709613@jb359-5 Lab1]$ Bank.cpp: In member function ‘void Bank::print_balances()’: -bash: syntax error near unexpected token('[004709613 @ jb359-5 Lab1] $ Bank.cpp:45:错误:'std :: operator <<中的'operator <<'不匹配[与_Traits = std :: char_traits](((std :: basic_ostream>&)(&std :: cout)),((const char*)"Savings:$"))<<((Bank*)this) - > Bank :: savings'/ usr/lib/gcc/x86_64 -redhat-linux/4.4.7 /../../../../ include/c ++/4.4.7/ostream:108:注意:候选人是:std :: basic_ostream <_CharT,_Traits>&std: :basic_ostream <_CharT,_Traits> :: operator <<(std :: basic_ostream <_CharT,_Traits>&()(std :: basic_ostream <_CharT,_Traits>&))[with _CharT = char,_ Traits = std :: char_traits ] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:117:注意:std :: basic_ostream <_CharT ,_Traits>&std :: basic_ostream <_CharT,_Traits> ::operator <<(std :: basic_ios <_CharT,_Traits>&()(std :: basic_ios <_CharT,_Traits>&))[with _CharT = char,_ Traits = std :: char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../ ../../include/c++/4.4.7/ostream:127:注意:std :: basic_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(std :: ios_base&()(std :: ios_base&))[with _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_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(long int)[with _CharT = char,_Traits = std :: char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:169:note:std: :basic_ostream <_CharT,_ Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(long unsigned int)[with _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_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT, _Traits> ::运算符<<(bool)[with _CharT = char,_Traits = std :: char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4 .7/bits/ostream.tcc:91:注意:std :: basic_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(short int)[with _CharT = char,_Traits = std :: char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:180:note:std :: basic_ostream <_CharT,_ Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(short unsigned int)[with _CharT = char,_Traits = std :: char_traits]/usr/lib/gcc/x86_64-redhat- linux/4.4.7 /../../../../ include/c ++/4.4.7/bits/ostream.tcc:105:注意:std :: basic_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(int)[with _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_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(unsigned int)[with _CharT = char,_Traits = std :: char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:200:note:std :: basic_ostream <_CharT, _Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(long long int)[with _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_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(long long unsigned int)[with _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_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(double)[with _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_ostream < _CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(float)[with _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_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(long double)[与_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_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(const void)[with _CharT = char,_ Traits = std :: char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7 /bits/ostream.tcc:119:注意:std :: basic_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(std :: basic_streambuf <_CharT,_Traits> )[with _CharT = char,_Traits = std :: char_traits] Bank.cpp:46:错误:'std :: operator <<中没有匹配'operator <<'[与_Traits = std :: char_traits]((std :: basic_ostream> &)(&std :: cout)),((const char)"Checking:$"))<<((Bank*)this) - > Bank :: checking'/ usr/lib/gcc/x86_64-redhat- linux/4.4.7 /../../../../ include/c ++/4.4.7/ostream:108:注意:候选人是:std :: basic_ostream <_CharT,_Traits>&std :: basic_ostream < _CharT,_Traits> :: operator <<(std :: basic_ostream <_CharT,_Traits>&()(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:117: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& ()(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:127: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& ()(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:165: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:169: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:173: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ostream.tcc:91: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:180: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ostream.tcc:105: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:191: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Tra
您需要为 Account 类定义 + 运算符,以便它知道要做什么。由于您尝试添加带有双精度的 Account 对象,因此您将需要这个。
double Account::operator+(const double value) const {
return balance + value;
}
Run Code Online (Sandbox Code Playgroud)
如果你想添加两个帐户对象,你可以重载它。
Account Account::operator+(const Account &other) const {
return Account(balance + other.balance, interest_rate);
}
Run Code Online (Sandbox Code Playgroud)
如果您不想在类中添加额外的运算符,那么请不要写:
savings = savings + amount;
Run Code Online (Sandbox Code Playgroud)
使用已经声明的Account类的方法并写:
savings.deposit(amount);
Run Code Online (Sandbox Code Playgroud)