一直报''没有与这些操作数匹配的 "<<" 运算符 "
```c++
#include
#include
#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) <withdraw(10.0) << endl;
cout << Account(1000.0).withdraw(1001.0) << endl;
};
};
```
望采纳!点击该回答右侧的“采纳”按钮即可采纳!!!兄弟你的代码错误不少呢!!!我给你等会给你列出来,有时间会帮你改代码,希望给博主个采纳呀!!!
一共四个问题!
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;
}
有几个问题
#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;
}
double withdraw(double amount) {
auto temp{ 0.0 };
if (balance < amount) {
temp = balance;
balance = 0;
return temp;
}
else {
balance -= amount;
return amount;
}
}
因为你的类Account 函数void withdraw没有返回,改成这样就ok,还有类结束那个大括号弄到main前面去
double withdraw(double amount)
{
auto temp{0.0};
if (balance < amount)
{
temp = balance;
balance = 0;
return temp;
}
else
{
balance -= amount;
return amount;
}
}
谢谢各位,现在问题已经解决