【问题标题】:Getting very long "No match for 'operator+'" error in C++在 C++ 中出现很长的“不匹配 'operator+'”错误
【发布时间】:2014-05-16 05:19:54
【问题描述】:

我没有练习过 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::savings + amount' 中的'operator+' 不匹配 Bank.cpp:19: 错误: '((Bank*)this)->Bank::checking += amount' 中的'operator+=' 不匹配 Bank.cpp:在成员函数“void Bank::withdraw(double, std::string)”中: Bank.cpp:26: 错误: '((Bank*)this)->Bank::savings -= amount' 中的'operator-=' 不匹配 Bank.cpp:28: 错误: '((Bank*)this)->Bank::checking -= amount' 中的'operator-=' 不匹配 Bank.cpp:在成员函数‘void Bank::transfer(double, std::string)’中: Bank.cpp:35: 错误: '((Bank*)this)->Bank::savings -= amount' 中的'operator-=' 不匹配 Bank.cpp:36: 错误: '((Bank*)this)->Bank::checking += amount' 中的'operator+=' 不匹配 Bank.cpp:38: 错误: '((Bank*)this)->Bank::checking -= amount' 中的'operator-=' 不匹配 Bank.cpp:39: 错误: '((Bank*)this)->Bank::savings += amount' 中的'operator+=' 不匹配 Bank.cpp:在成员函数“void Bank::print_balances()”中: Bank.cpp:45:错误:'std::operator&)(& std::cout) 中的 'operatorBank::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>&)) [ _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>&)) [with _CharT = 字符,_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&)) [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& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator) [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) [with _CharT = char, _Traits = std::char_traits] Bank.cpp:46: 错误: 'std::operator&)(& std::cout) 中的 'operator)"检查:$")) 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>&)) [ _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>&)) [with _CharT = 字符,_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&)) [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& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator) [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) [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: error: no match for ‘operator+=’ in ‘((Bank)this)->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: error: no match for ‘operator-=’ in ‘((Bank*)this)->Bank::savings -= 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:36: error: no match for ‘operator+=’ in ‘((Bank*)this)->Bank::checking += 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: error: no match for ‘operator+=’ in ‘((Bank*)this)->Bank::savings += amount’ -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: error: no match for 'operator &)(& std::cout)), ((const char*)"Savings: $")) 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>&)) [ _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>&)) [with _CharT = 字符,_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&)) [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& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator) [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) [with _CharT = char, _Traits = std::char_traits] Bank.cpp:46: 错误: 'std::operator&)(& std::cout) 中的 'operator)"检查:$")) 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>&)) [ _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>&)) [with _CharT = 字符,_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&)) [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& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator) [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*) [with _CharT = char, _Traits = std::char_traits]

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

【问题讨论】:

  • savingschecking 的任何类型都不支持这样的算术或输出。
  • 请贴出头文件,让我们看看savings是什么类型
  • 你需要为 operator+ 添加一个成员函数,它说明你想要它做什么。
  • 重申一下:储蓄和支票有哪些类型? Bank.cpp 第 17 行是空白的,您确定您发布的代码正是您尝试编译的代码吗?
  • 储蓄和支票的类型是Account,这是我写的另一个类,编译得很好。我觉得我在这里做的事情很明显是错误的。此外,Bank.cpp 文件中的第 17 行实际上是我在此处发布的代码中的第 12 行。我删除了我课程所需的文件顶部的一些 cmets。很抱歉造成混乱。

标签: c++


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您需要为 Account 类定义 + 运算符,以便它知道要做什么。由于您尝试使用双精度添加 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);
    }
    

    【讨论】:

    • Account 没有接受单个 double 作为输入的构造函数或 = 运算符,因此第一个运算符不会像您显示的那样编译,因为结果被分配给Accountm 所以它需要一个 Account 结果。在第二个运算符中,不要使用new,我建议使用this.interest_rate 而不是0。
    • 是的,在我发布它之后才意识到。现在应该修好了。用 Java 编写了这么多代码,以至于我不假思索地输入了 new。
    【解决方案3】:

    在您的源代码的这一行中:

                savings = savings + amount;
    

    savings 的类型是 Account,但 amountdouble。除非您在 Account 类上定义 + 成员运算符,例如

    Account operator+(const double value) const;
    

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

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

    那么你不能做这样的添加。

    【讨论】:

      【解决方案4】:

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

      savings = savings + amount;
      

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

      savings.deposit(amount);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-10
        • 2013-07-31
        • 1970-01-01
        • 2011-10-06
        • 2011-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多