在线银行C++项目编译错误:类型限定符不兼容与抽象类实例化问题
解决在线银行服务项目的两处编译错误
错误1:对象的类型限定符与成员函数Customer::displayCustomer不兼容
原因
Account::displayAccount()是const成员函数,在该函数中成员变量user会被视为const Customer类型。但Customer::displayCustomer()未声明为const成员函数,const对象无法调用非const成员函数,导致类型不兼容。
修复方案
修改Customer类的displayCustomer()方法,将其声明并定义为const成员函数:
修改后的Customer.h
#ifndef CUSTOMER_H #define CUSTOMER_H #include <string> class Customer { public: Customer(); Customer(const std::string& firstName, const std::string& lastName, const std::string& address, const std::string& email); void displayCustomer() const; // 添加const修饰符 private: std::string firstName; std::string lastName; std::string address; std::string email; }; #endif // CUSTOMER_H
修改后的Customer.cpp
#include "Customer.h" #include <iostream> #include <iomanip> Customer::Customer() { // 默认构造函数 } Customer::Customer(const std::string& firstName, const std::string& lastName, const std::string& address, const std::string& email) : firstName(firstName), lastName(lastName), address(address), email(email) { // 重载构造函数 } void Customer::displayCustomer() const { // 添加const修饰符 std::cout << "Name: " << firstName << " " << lastName << std::endl; std::cout << "Address: " << address << std::endl; std::cout << "Email: " << email << std::endl; }
错误2:不允许抽象类类型SavingsAccount的对象
原因
基类Account包含两个纯虚函数:virtual bool deposit(double amount) = 0;和virtual bool withdraw(double amount) = 0;。继承自Account的子类必须实现这两个纯虚函数,否则子类会被标记为抽象类,无法实例化对象。当前SavingsAccount未实现这两个函数,因此触发编译错误。
修复方案
在SavingsAccount类中添加deposit和withdraw函数的声明与实现:
修改后的SavingsAccount.h
#ifndef SAVINGSACCOUNT_H #define SAVINGSACCOUNT_H #include "Account.h" class SavingsAccount : public Account { public: SavingsAccount(); SavingsAccount(int accountNumber, double balance, const Customer& user, double interestRate); double getInterestRate() const; double calcInterest(); bool deposit(double amount) override; // 添加函数声明 bool withdraw(double amount) override; // 添加函数声明 void displayAccount() const override; private: double interestRate; }; #endif // SAVINGSACCOUNT_H
修改后的SavingsAccount.cpp
#include "SavingsAccount.h" #include <iostream> #include <iomanip> SavingsAccount::SavingsAccount() : interestRate(0.02) { // 默认构造函数 } SavingsAccount::SavingsAccount(int accountNumber, double balance, const Customer& user, double interestRate) : Account(accountNumber, balance, user), interestRate(interestRate) { // 重载构造函数 } double SavingsAccount::getInterestRate() const { return interestRate; } double SavingsAccount::calcInterest() { double interest = getBalance() * interestRate; setBalance(getBalance() + interest); return interest; } bool SavingsAccount::deposit(double amount) { if (amount >= 0) { setBalance(getBalance() + amount); return true; } return false; } bool SavingsAccount::withdraw(double amount) { if (amount >= 0 && amount <= getBalance()) { setBalance(getBalance() - amount); return true; } return false; } void SavingsAccount::displayAccount() const { std::cout << "Savings Account ----" << std::endl; Account::displayAccount(); std::cout << "Interest rate: " << interestRate * 100 << "%" << std::endl; }
完成上述修改后,两处编译错误即可解决。
内容的提问来源于stack exchange,提问作者gbolive011
相关产品推荐
相关产品推荐

