在C中得到很长的“不匹配'operator+'”错误

我没有参加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;
}

这是头文件:

#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:错误:'((Bank *)this'中'运算符'不匹配 - > Bank ::储蓄金额'Bank.cpp:19:错误:'(=(Bank *)中'运营商='不匹配 - >银行::支票=金额'Bank.cpp:成员函数'void Bank :: withdraw(double,std :: string)':Bank.cpp:26:错误:'((Bank *)this中'operator- ='不匹配 - > Bank :: savings - = amount'Bank.cpp: 28:错误:'((Bank )这里的'operator- ='不匹配 - > Bank :: checking - = amount'Bank.cpp:在成员函数'void Bank :: transfer(double,std ::)字符串)':Bank.cpp:35:错误:'((银行)这里的'operator- ='不匹配 - >银行::储蓄 - =金额'Bank.cpp:36:错误:不匹配'operator ='in'((Bank *)this) - > Bank :: checking = amount'Bank.cpp:38:错误:不匹配'operator- ='in'((Bank *)this) - > Bank :: checking - = amount'Bank.cpp:39:错误:在'((Bank *)this'中'operator ='不匹配 - > Bank :: sav ings = amount'Bank.cpp:在成员函数中'void Bank :: print_balances()':Bank.cpp:45:错误:'std :: operator <<中的'operator <<'不匹配[with _Traits = std :: char_traits](((std :: basic_ostream>&)(&std :: cout)),((const char *)“Savings:$”))<<((Bank *)this) - > Bank ::储蓄'/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 :: basi c_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:注意: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> :: 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 :: b asic_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:注意: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 < <(很长一段时间NT)[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)[with _CharT = char,_Traits = std :: char_traits] / usr / lib中/ GC c / 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 ::检查'/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>&)) [与_CharT = char,_Trait s = 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:注意:std :: basic_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT ,_Traits> :: operator <<(long unsigned int)[机智h 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> :: 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:注意: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:注意: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)[with _CharT = char,_Traits = std :: char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4 0.7 /../../../../包括/ 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:note: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:错误:'((银行)这里的'运营商'不匹配 - >银行::储蓄金额'-bash:意外令牌附近的语法错误('[004709613 @ jb359-5 Lab1] $ Bank . cpp:19:错误:'(=(Bank)this'中的'operator ='不匹配 - > Bank :: checking = amount'-bash:意外令牌附近的语法错误('[004709613 @ jb359-5 Lab1] $ Bank .cpp:在成员函数'void Bank :: withd中raw(double,std :: string)': - bash:意外令牌附近的语法错误('[004709613 @ jb359-5 Lab1] $ Bank.cpp:26:错误:'(=)中的'operator- ='不匹配(( Bank *)this) - > Bank :: savings - = amount'-bash:意外令牌附近的语法错误('[004709613 @ jb359-5 Lab1] $ Bank.cpp:28:错误:'operator- ='不匹配in'((Bank *)this) - > Bank :: checking - = amount'-bash:意外令牌附近的语法错误('[004709613 @ jb359-5 Lab1] $ Bank.cpp:在成员函数'void Bank :: transfer(double,std :: string)': - bash:意外令牌附近的语法错误('[004709613 @ jb359-5 Lab1] $ Bank.cpp:35:错误:'(=)中的'operator- ='不匹配(( Bank *)this) - > Bank :: savings - = amount'-bash:意外令牌附近的语法错误('[004709613 @ jb359-5 Lab1] $ Bank.cpp:36:错误:不匹配'operator ='in '((Bank *)this) - > Bank :: checking = amount'-bash:意外令牌附近的语法错误('[004709613 @ jb359-5 Lab1] $ Bank.cpp:38:错误:不匹配'运算符 - ='in'((Bank )this) - > Bank ::检查 - =金额'-bash:意外令牌附近的语法错误('[004709613 @ jb359-5 Lab1] $ Bank.cpp:39:错误:'((银行)这里的'运营商='不匹配 - > Bank :: savings = amount'-bash:意外令牌附近的语法错误('[004709613 @ jb359-5 Lab1] $ Bank.cpp:在成员函数'void Bank :: print_balances()'中:-bash:语法错误接近意外token('[004709613 @ jb359-5 Lab1] $ Bank.cpp:45:错误:'std :: operator <<中没有匹配'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:注意:候选人是: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:注意: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> :: 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:注意:std :: basic_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT, _Traits> ::运算符<<( 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:note: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,_Tra它= 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)[with _CharT = char, _Traits = std :: char_traits] /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c /4.4.7/ostream:225:note: 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>&) ) 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: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:注意: 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> :: 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:注意: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:注意: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:注意: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)[with _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]

谁知道我做错了什么?我确定这是非常愚蠢的事情 . 我也可以发布头文件,但由于远程编辑问题,很难这样做 .

回答(4)

2 years ago

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

savings = savings + amount;

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

savings.deposit(amount);

2 years ago

您需要为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);
}

2 years ago

我怀疑你宣布保存和检查私人成员为“帐户”而不是加倍 . 即使这是您的预期设计,也无法在没有expliclity定义运算符(const Account&)的情况下添加用户定义的类对象 .

2 years ago

在这行源代码中:

savings = savings + amount;

savings 的类型为 Account ,但 amountdouble . 除非你在 Account 类上定义一个 + 成员运算符

Account operator+(const double value) const;

或者全局 + 运算符(可能作为 friend 函数)之类的

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

那么你不能做这样的补充 .