[Error] cannot dynamic_cast '*(((ATM*)this)->ATM::accounts + ((sizetype)(((long long unsigned int)i) * 72ull)))' (of type 'class Account') to type 'class Debit*' (source is not a pointer)
时间: 2023-06-10 19:04:08 浏览: 156
这个错误提示是什么意思?
这个错误提示的意思是无法将类型为“Account”的动态转换为类型为“Debit *”的指针,因为源不是指针。这说明在程序中尝试将一个Account对象转换为Debit指针,但是源不是指针类型,因此无法进行转换。可能需要检查程序中的数据类型或指针问题。
相关问题
根据类图,实现java代码:@startuml class BankAccount { -accountNumber : String -balance : double -ownerName : String +BankAccount(accountNumber:String, ownerName:String) +getAccountNumber():String +getBalance():double +getOwnerName():String +deposit(amount:double):void +withdraw(amount:double):void } class BankAccountDAO { -accounts : Map<String, BankAccount> -fileName : String +BankAccountDAO(fileName:String) +getAccount(accountNumber:String):BankAccount +getAllAccounts():List<BankAccount> +addAccount(account:BankAccount):void +updateAccount(account:BankAccount):void +deleteAccount(accountNumber:String):void -saveToFile():void } BankAccount -> BankAccountDAO @enduml
```
public class BankAccount {
private String accountNumber;
private double balance;
private String ownerName;
public BankAccount(String accountNumber, String ownerName) {
this.accountNumber = accountNumber;
this.ownerName = ownerName;
this.balance = 0.0;
}
public String getAccountNumber() {
return accountNumber;
}
public double getBalance() {
return balance;
}
public String getOwnerName() {
return ownerName;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
balance -= amount;
}
}
import java.io.*;
import java.util.*;
public class BankAccountDAO {
private Map<String, BankAccount> accounts;
private String fileName;
public BankAccountDAO(String fileName) {
this.fileName = fileName;
accounts = new HashMap<String, BankAccount>();
loadFromFile();
}
public BankAccount getAccount(String accountNumber) {
return accounts.get(accountNumber);
}
public List<BankAccount> getAllAccounts() {
return new ArrayList<BankAccount>(accounts.values());
}
public void addAccount(BankAccount account) {
accounts.put(account.getAccountNumber(), account);
saveToFile();
}
public void updateAccount(BankAccount account) {
accounts.put(account.getAccountNumber(), account);
saveToFile();
}
public void deleteAccount(String accountNumber) {
accounts.remove(accountNumber);
saveToFile();
}
private void loadFromFile() {
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
String accountNumber = parts[0];
String ownerName = parts[1];
double balance = Double.parseDouble(parts[2]);
BankAccount account = new BankAccount(accountNumber, ownerName);
account.deposit(balance);
accounts.put(accountNumber, account);
}
reader.close();
} catch (IOException e) {
System.out.println("Failed to load accounts from file: " + e.getMessage());
}
}
private void saveToFile() {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
for (BankAccount account : accounts.values()) {
String line = account.getAccountNumber() + "," + account.getOwnerName() + "," + account.getBalance();
writer.write(line);
writer.newLine();
}
writer.close();
} catch (IOException e) {
System.out.println("Failed to save accounts to file: " + e.getMessage());
}
}
}
```
阅读全文