繁体   English   中英

在C ++中获得很长的“不匹配'运算符+'”错误

[英]Getting very long “No match for 'operator+'” error in C++

我没有使用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.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: :string)':Bank.cpp:35:错误:'((Bank *)这里的'operator- ='不匹配 - > Bank :: savings - = amount'Bank.cpp:36:错误:不匹配for'operator + ='in'((Bank *)this) - > Bank :: checking + = amount'Bank.cpp:38:error:'match- ='in'((Bank *)this) - >银行:检查 - =金额'Bank.cpp:39:错误:'运营商+ ='不匹配'((银行*)这个) - >银行::储蓄+ =金额'Bank.cpp:成员函数'void Bank :: print_balances()': 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_bas e&( )(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:注意:std :: basic_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(short int) [_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 :注意: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的-红帽-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:note: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 :: b asic_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:错误:不匹配'operator <<'in 'std :: operator << [with _Traits = std :: char_traits](((std :: basic_ostream>&)(&std :: cout)),((const char )“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/../../../ .. /包括/ 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:注意: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.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: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:错误:在'((Bank )this中'运算符+ ='不匹配 - > Bank :: checking + = (' [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: In member function 'void Bank::withdraw(double, std::string)': -bash: syntax error near unexpected token ('[004709613 @ jb359-5 Lab1] $ Bank.cpp:26:错误:'((Bank *)这里的'operator- ='不匹配 - > Bank :: savings - = (' [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:28: error: no match for 'operator-=' in '((Bank*)this)->Bank::checking -= amount' -bash: syntax error near unexpected token 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-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: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:错误:在'((Bank *)this中'运营商+ ='不匹配 - > Bank :: checking + = (' [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:38: error: no match for 'operator-=' in '((Bank*)this)->Bank::checking -= amount' -bash: syntax error near unexpected token amount'-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:错误:'运算符+ ='不匹配'(( Bank *)this) - > Bank :: savings + = (' [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: 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:注意:candi 日期是:std :: basic_ostream <_CharT,_Traits>&std :: basic_ostream <_CharT,_Traits> :: operator <<(std :: basic_ostream <_CharT,_Traits>&( )(std :: basic_ostream <_CharT,_Traits> &))[与_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: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的-红帽-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:没有 te: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 <_C harT,_Traits> :: operator <<(std :: basic_streambuf <_CharT,_Traits> )[with _CharT = char,_Traits = std :: char_traits] Bank.cpp:46:错误:不匹配'operator <<'in 'std :: operator << [with _Traits = std :: char_traits](((std :: basic_ostream>&)(&std :: cout)),((const char )“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:注意: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:注意:std :: basic_o stream <_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,_ Tra 它的= 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]

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

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

savings = savings + amount;

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

savings.deposit(amount);

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

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

在这行源代码中:

            savings = savings + amount;

savings有一种Account ,但amountdouble 除非你在Account类上定义一个+成员运算符

Account operator+(const double value) const;

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM