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日