该对象具有与成员函数“Customer::d isplayCustomer”不兼容的类型限定符

the object has type qualifiers that are not compatible with the member function "Customer::displayCustomer"

提问人:gbolive011 提问时间:9/18/2023 最后编辑:Vlad from Moscowgbolive011 更新时间:9/18/2023 访问量:52

问:

我目前正在为网上银行服务做一个项目,无论我尝试什么,我都会在这里遇到同样的问题。我不断收到错误的行在 Account.cpp 文件中进行了注释

客户.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();

private:
    std::string firstName;
    std::string lastName;
    std::string address;
    std::string email;
};

#endif // CUSTOMER_H

客户.cpp:

#include "Customer.h"
#include <iostream>
#include <iomanip>

Customer::Customer() {
    // Default constructor
}

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) {
    // Overloaded constructor
}

void Customer::displayCustomer() {
    std::cout << "Name: " << firstName << " " << lastName << std::endl;
    std::cout << "Address: " << address << std::endl;
    std::cout << "Email: " << email << std::endl;
}

帐户.h:

#ifndef ACCOUNT_H
#define ACCOUNT_H
#include "Customer.h"

class Account {

public:

    Account();

    Account(int accountNumber, double balance, const Customer& user);

    double getBalance() const;

    void setBalance(double balance);

    virtual bool deposit(double amount) = 0;

    virtual bool withdraw(double amount) = 0;

    virtual void displayAccount() const;

protected:

    int accountNumber;

    double balance;

    Customer user;

};

#endif // ACCOUNT_H

帐户.cpp:

#include "Account.h"
#include <iostream>
#include <iomanip>

Account::Account() {

    // Default constructor

}

Account::Account(int accountNumber, double balance, const Customer& user)

    : accountNumber(accountNumber), balance(balance), user(user) {

    // Overloaded constructor

}

double Account::getBalance() const {

    return balance;

}

void Account::setBalance(double balance) {

    this->balance = balance;

}

void Account::displayAccount() const {

    std::cout << "Account number: " << accountNumber << std::endl;

    user.displayCustomer();

    std::cout << "Balance: $" << std::fixed << std::setprecision(2) << balance << std::endl;

}

CheckingAccount.h:

#ifndef CHECKINGACCOUNT_H
#define CHECKINGACCOUNT_H

#include "Account.h"

class CheckingAccount : public Account {
public:
    CheckingAccount();
    CheckingAccount(int accountNumber, double balance, const Customer& user, double transactionFee);
    double getTransactionFee() const;
    bool deposit(double amount) override;
    bool withdraw(double amount) override;
    void displayAccount() const override;

private:
    double transactionFee;
};

#endif // CHECKINGACCOUNT_H

CheckingAccount.cpp:

#include "CheckingAccount.h"
#include <iostream>
#include <iomanip>

CheckingAccount::CheckingAccount()
    : transactionFee(5.0) {
    // Default constructor
}

CheckingAccount::CheckingAccount(int accountNumber, double balance, const Customer& user, double transactionFee)
    : Account(accountNumber, balance, user), transactionFee(transactionFee) {
    // Overloaded constructor
}

double CheckingAccount::getTransactionFee() const {
    return transactionFee;
}

bool CheckingAccount::deposit(double amount) {
    if (amount >= 0) {
        double newBalance = getBalance() + amount - transactionFee;
        setBalance(newBalance);
        return true;
    }
    return false;
}

bool CheckingAccount::withdraw(double amount) {
    if (amount >= 0 && amount <= getBalance()) {
        double newBalance = getBalance() - amount - transactionFee;
        setBalance(newBalance);
        return true;
    }
    return false;
}

void CheckingAccount::displayAccount() const {
    std::cout << "Checking Account ----" << std::endl;
    Account::displayAccount();
    std::cout << "Transaction fee: $" << std::fixed << std::setprecision(2) << transactionFee << std::endl;
}

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();
    void displayAccount() const override;

private:
    double interestRate;
};

#endif // SAVINGSACCOUNT_H

储蓄账户.cpp:

#include "SavingsAccount.h"
#include <iostream>
#include <iomanip>

SavingsAccount::SavingsAccount()
    : interestRate(0.02) {
    // Default constructor
}

SavingsAccount::SavingsAccount(int accountNumber, double balance, const Customer& user, double interestRate)
    : Account(accountNumber, balance, user), interestRate(interestRate) {
    // Overloaded constructor
}

double SavingsAccount::getInterestRate() const {
    return interestRate;
}

double SavingsAccount::calcInterest() {
    double interest = getBalance() * interestRate;
    setBalance(getBalance() + interest);
    return interest;
}

void SavingsAccount::displayAccount() const {
    std::cout << "Savings Account ----" << std::endl;
    Account::displayAccount();
    std::cout << "Interest rate: " << interestRate * 100 << "%" << std::endl;
}

测试器.cpp:

#include <iostream>
#include <iomanip>
#include "Account.h"
#include "CheckingAccount.h"
#include "SavingsAccount.h"

using namespace std;

int main() {
    int selection = 0;
    string firstName, lastName, address, email;
    double amount;

    // Input user's information
    cout << "Enter your first name: ";
    cin >> firstName;
    cout << "Enter your last name: ";
    cin >> lastName;
    cout << "Enter your address: ";
    cin.ignore();
    getline(cin, address);
    cout << "Enter your email: ";
    cin >> email;

    // Create a Customer object
    Customer customer(firstName, lastName, address, email);

    // Create CheckingAccount and SavingsAccount objects
    CheckingAccount checkingAccount(1111, 0.0, customer, 5.0);
    SavingsAccount savingsAccount(2222, 0.0, customer, 0.02); // Also running into error here "object of abstract class type "SavingsAccount" is not allowed

    cout << "Your checking account and savings account are created successfully." << endl << endl;

    while (selection != 8) {
        cout << "Please select a service from the menu:" << endl;
        cout << "1. Display accounts" << endl;
        cout << "2. Deposit to checking account" << endl;
        cout << "3. Deposit to savings account" << endl;
        cout << "4. Withdraw from checking account" << endl;
        cout << "5. Withdraw from savings account" << endl;
        cout << "6. Transfer money from checking to savings" << endl;
        cout << "7. Transfer money from savings to checking" << endl;
        cout << "8. Exit" << endl;
        cin >> selection;

        switch (selection) {
            case 1:
                checkingAccount.displayAccount();
                savingsAccount.displayAccount();
                break;
            case 2:
                cout << "Enter the amount to deposit to checking account: ";
                cin >> amount;
                if (checkingAccount.deposit(amount)) {
                    cout << "Deposit is successful." << endl;
                } else {
                    cout << "Invalid deposit amount." << endl;
                }
                break;
            case 3:
                cout << "Enter the amount to deposit to savings account: ";
                cin >> amount;
                if (savingsAccount.deposit(amount)) {
                    cout << "Deposit is successful." << endl;
                } else {
                    cout << "Invalid deposit amount." << endl;
                }
                break;
            case 4:
                cout << "Enter the amount to withdraw from checking account: ";
                cin >> amount;
                if (checkingAccount.withdraw(amount)) {
                    cout << "Withdraw is successful." << endl;
                } else {
                    cout << "Invalid withdrawal amount or insufficient balance." << endl;
                }
                break;
            case 5:
                cout << "Enter the amount to withdraw from savings account: ";
                cin >> amount;
                if (savingsAccount.withdraw(amount)) {
                    cout << "Withdraw is successful." << endl;
                } else {
                    cout << "Invalid withdrawal amount or insufficient balance." << endl;
                }
                break;
            case 6:
                cout << "Enter the amount to transfer from checking to savings: ";
                cin >> amount;
                if (checkingAccount.withdraw(amount) && savingsAccount.deposit(amount)) {
                    cout << "Transfer is successful." << endl;
                } else {
                    cout << "Invalid transfer amount or insufficient balance in checking account." << endl;
                }
                break;
            case 7:
                cout << "Enter the amount to transfer from savings to checking: ";
                cin >> amount;
                if (savingsAccount.withdraw(amount) && checkingAccount.deposit(amount)) {
                    cout << "Transfer is successful." << endl;
                } else {
                    cout << "Invalid transfer amount or insufficient balance in savings account." << endl;
                }
                break;
            case 8:
                cout << "Thanks for using Bank of Wake!" << endl;
                break;
            default:
                cout << "Invalid selection." << endl;
                break;
        }
    }

    return 0;
}
C++ 常量 成员函数

评论

0赞 πάντα ῥεῖ 9/18/2023
你真的确定这包括这里要求的实际问题的最小可重现示例吗?我敢打赌,你发布的大部分代码都是完全无关紧要的。
0赞 gbolive011 9/18/2023
我主要想看看错误是否以某种方式连接到项目中的任何其他文件。此外,由于某种原因,我想要添加的注释没有显示在我想要添加它的文件中。它在帐户 .cpp 中:
0赞 gbolive011 9/18/2023
user.displayCustomer();这是“void Account::d isplayAccount()”类中的问题行
0赞 πάντα ῥεῖ 9/18/2023
你的编译器没有告诉你错误的确切来源吗?
1赞 Retired Ninja 9/18/2023
displayCustomer不是 const 函数,因此您不能从另一个 const 函数内部调用它。解决方案是使其像此代码中的许多其他函数一样常量。

答:

0赞 Vlad from Moscow 9/18/2023 #1

不能从常量成员函数调用非常量成员函数。

在类中,成员函数是一个非常量成员函数:CustomerdisplayCustomer

void displayCustomer();

但它是从常量成员函数调用的,例如从类的函数调用displayAccountAccount

void Account::displayAccount() const {

    std::cout << "Account number: " << accountNumber << std::endl;

    user.displayCustomer();

    std::cout << "Balance: $" << std::fixed << std::setprecision(2) << balance << std::endl;

}

在常量成员函数中,为其调用函数的对象被视为常量对象。也就是说,使用类型的指针访问此类对象的成员。从指向常量对象的指针到指向非常量对象的指针,不存在隐式转换。thisconst class_object *

将成员函数声明为常量函数displayCustomer

void displayCustomer() const;