簡體   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