Jimmy0329 2022-12-27 12:04 采纳率: 72.7%
浏览 27
已结题

关于#c++#的问题,请各位专家解答!

一直报''没有与这些操作数匹配的 "<<" 运算符 "


```c++
#include<iostream>
#include<string>
#pragma once
using namespace std;
class Account {
    double balance;
public:
    Account() {
        balance = 0.0;
    }
    Account(double balance_) {
        balance = balance_;
    }
    void deposit(double amount) {
        balance += amount;
    };
    void withdraw(double amount) {
        auto temp{ 0.0 };
        if (balance < amount) {
            temp = balance;
            balance = 0;
            return; temp;
        }
        else {
            balance -= amount;
            return; amount;
        }
    }

    int main() {
        Account a1;
        Account a2 = Account(100.0);
        a1.deposit(9.0);
        cout << a1.withdraw(9.0) <<endl;
        cout << a2.withdraw(10.0) << endl;
        cout << Account(1000.0).withdraw(1001.0) << endl;
    };
};


```

  • 写回答

4条回答 默认 最新

  • heart_6662 2022-12-27 12:30
    关注

    望采纳!点击该回答右侧的“采纳”按钮即可采纳!!!兄弟你的代码错误不少呢!!!我给你等会给你列出来,有时间会帮你改代码,希望给博主个采纳呀!!!
    一共四个问题!
    1.auto temp{ 0.0 } 中的花括号应改为小括号。
    2.在 withdraw 函数中,当 balance 小于 amount 时,return; temp; 应改为 return temp;,否则应改为 return amount;。
    3.int main() 函数中的 cout << Account(1000.0).withdraw(1001.0) << endl; 中的 Account(1000.0) 应改为 Account a3(1000.0),否则会出现“未解引用的指针”的错误。
    4.在类定义的最后应加上 };,否则会出现“未定义的标识符”的错误。

    修改后的代码如下:

    #include<iostream>
    #include<string>
    #pragma once
    using namespace std;
    
    class Account {
      double balance;
      public:
        Account() {
          balance = 0.0;
        }
        Account(double balance_) {
          balance = balance_;
        }
        void deposit(double amount) {
          balance += amount;
        }
        double withdraw(double amount) {
          if (balance < amount) {
            balance = 0;
            return balance;
          }
          else {
            balance -= amount;
            return amount;
          }
        }
    };
    
    int main() {
      Account a1;
      Account a2 = Account(100.0);
      a1.deposit(9.0);
      cout << a1.withdraw(9.0) <<endl;
      cout << a2.withdraw(10.0) << endl;
      cout << Account(1000.0).withdraw(1001.0) << endl;
      return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 1月5日
  • 已采纳回答 12月28日
  • 创建了问题 12月27日

悬赏问题

  • ¥15 angular开发过程中,想要读取模型文件,即图1的335行,会报404错误(如图2)。但我的springboot里配置了静态资源文件,如图3。且在该地址下我有模型文件如图4,请问该问题该如何解决呢?
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置
  • ¥15 Java计划序号重编制功能,此功能会对所有序号重新排序,排序后不改变前后置关系。
  • ¥15 关于哈夫曼树应用得到一些问题